{"id":161,"date":"2025-02-21T07:24:56","date_gmt":"2025-02-21T07:24:56","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=161"},"modified":"2025-02-21T07:24:56","modified_gmt":"2025-02-21T07:24:56","slug":"python-exception-handling-xu-ly-ngoai-le-trong-python","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=161","title":{"rendered":"Python Exception Handling \u2013 X\u1eed l\u00fd ngo\u1ea1i l\u1ec7 trong Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"821\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44-1024x821.png\" alt=\"\" class=\"wp-image-167\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44-1024x821.png 1024w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44-300x241.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44-768x616.png 768w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44-850x682.png 850w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-44.png 1242w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Trong l\u1eadp tr\u00ecnh, l\u1ed7i (error) l\u00e0 \u0111i\u1ec1u kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi. N\u1ebfu kh\u00f4ng x\u1eed l\u00fd t\u1ed1t, ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 b\u1ecb d\u1eebng \u0111\u1ed9t ng\u1ed9t, g\u00e2y \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng v\u00e0 hi\u1ec7u su\u1ea5t h\u1ec7 th\u1ed1ng. Trong Python, exception handling l\u00e0 m\u1ed9t c\u01a1 ch\u1ebf gi\u00fap ph\u00e1t hi\u1ec7n v\u00e0 x\u1eed l\u00fd l\u1ed7i m\u1ed9t c\u00e1ch an to\u00e0n m\u00e0 kh\u00f4ng l\u00e0m gi\u00e1n \u0111o\u1ea1n to\u00e0n b\u1ed9 ch\u01b0\u01a1ng tr\u00ecnh.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Exception l\u00e0 g\u00ec?<\/h2>\n\n\n\n<p>Exception (ngo\u1ea1i l\u1ec7) l\u00e0 nh\u1eefng l\u1ed7i x\u1ea3y ra trong qu\u00e1 tr\u00ecnh ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh. Ch\u00fang kh\u00e1c v\u1edbi l\u1ed7i c\u00fa ph\u00e1p (syntax errors), v\u00ec l\u1ed7i c\u00fa ph\u00e1p x\u1ea3y ra ngay khi b\u1ea1n vi\u1ebft m\u00e3 sai, c\u00f2n ngo\u1ea1i l\u1ec7 ch\u1ec9 x\u1ea3y ra khi ch\u01b0\u01a1ng tr\u00ecnh ch\u1ea1y.<\/p>\n\n\n\n<p>M\u1ed9t s\u1ed1 l\u1ed7i ngo\u1ea1i l\u1ec7 ph\u1ed5 bi\u1ebfn trong Python:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Ngo\u1ea1i l\u1ec7<\/th><th>M\u00f4 t\u1ea3<\/th><\/tr><\/thead><tbody><tr><td><code>ZeroDivisionError<\/code><\/td><td>Chia m\u1ed9t s\u1ed1 cho 0.<\/td><\/tr><tr><td><code>ValueError<\/code><\/td><td>Ki\u1ec3u d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7.<\/td><\/tr><tr><td><code>FileNotFoundError<\/code><\/td><td>Kh\u00f4ng t\u00ecm th\u1ea5y t\u1ec7p tin.<\/td><\/tr><tr><td><code>IndexError<\/code><\/td><td>Truy c\u1eadp ph\u1ea7n t\u1eed ngo\u00e0i ph\u1ea1m vi danh s\u00e1ch.<\/td><\/tr><tr><td><code>KeyError<\/code><\/td><td>Truy c\u1eadp m\u1ed9t kh\u00f3a kh\u00f4ng t\u1ed3n t\u1ea1i trong dictionary.<\/td><\/tr><tr><td><code>TypeError<\/code><\/td><td>S\u1eed d\u1ee5ng ki\u1ec3u d\u1eef li\u1ec7u sai trong ph\u00e9p to\u00e1n.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>V\u00ed d\u1ee5 l\u1ed7i khi chia m\u1ed9t s\u1ed1 cho 0:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(10 \/ 0)  # L\u1ed7i ZeroDivisionError<br><\/code><\/pre>\n\n\n\n<p>L\u1ed7i x\u1ea3y ra ngay l\u1eadp t\u1ee9c, khi\u1ebfn ch\u01b0\u01a1ng tr\u00ecnh d\u1eebng ho\u1ea1t \u0111\u1ed9ng.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">2. X\u1eed l\u00fd ngo\u1ea1i l\u1ec7 v\u1edbi try-except<\/h2>\n\n\n\n<p>\u0110\u1ec3 tr\u00e1nh ch\u01b0\u01a1ng tr\u00ecnh b\u1ecb d\u1eebng, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <code>try-except<\/code> \u0111\u1ec3 b\u1eaft l\u1ed7i v\u00e0 x\u1eed l\u00fd ch\u00fang.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<br>    print(10 \/ 0)  # Th\u1eed th\u1ef1c hi\u1ec7n ph\u00e9p chia<br>except ZeroDivisionError:<br>    print(\"L\u1ed7i: Kh\u00f4ng th\u1ec3 chia m\u1ed9t s\u1ed1 cho 0!\")<br><\/code><\/pre>\n\n\n\n<p>Khi c\u00f3 l\u1ed7i x\u1ea3y ra, ch\u01b0\u01a1ng tr\u00ecnh kh\u00f4ng b\u1ecb crash, m\u00e0 thay v\u00e0o \u0111\u00f3, <code>except<\/code> s\u1ebd \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 b\u1eaft l\u1ed7i khi nh\u1eadp sai d\u1eef li\u1ec7u:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<br>    age = int(input(\"Nh\u1eadp tu\u1ed5i c\u1ee7a b\u1ea1n: \"))  # Y\u00eau c\u1ea7u nh\u1eadp s\u1ed1 nguy\u00ean<br>    print(f\"Tu\u1ed5i c\u1ee7a b\u1ea1n l\u00e0 {age}\")<br>except ValueError:<br>    print(\"L\u1ed7i: B\u1ea1n ph\u1ea3i nh\u1eadp m\u1ed9t s\u1ed1 nguy\u00ean!\")<br><\/code><\/pre>\n\n\n\n<p>N\u1ebfu ng\u01b0\u1eddi d\u00f9ng nh\u1eadp ch\u1eef thay v\u00ec s\u1ed1, <code>ValueError<\/code> s\u1ebd \u0111\u01b0\u1ee3c b\u1eaft, v\u00e0 ch\u01b0\u01a1ng tr\u00ecnh v\u1eabn ti\u1ebfp t\u1ee5c ch\u1ea1y b\u00ecnh th\u01b0\u1eddng.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">3. X\u1eed l\u00fd nhi\u1ec1u ngo\u1ea1i l\u1ec7 v\u1edbi nhi\u1ec1u except<\/h2>\n\n\n\n<p>Python cho ph\u00e9p b\u1ea1n x\u1eed l\u00fd nhi\u1ec1u lo\u1ea1i l\u1ed7i kh\u00e1c nhau b\u1eb1ng c\u00e1ch d\u00f9ng nhi\u1ec1u <code>except<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<br>    x = int(input(\"Nh\u1eadp m\u1ed9t s\u1ed1: \"))  # Ng\u01b0\u1eddi d\u00f9ng nh\u1eadp s\u1ed1<br>    print(10 \/ x)  # Th\u1ef1c hi\u1ec7n ph\u00e9p chia<br>except ZeroDivisionError:<br>    print(\"L\u1ed7i: Kh\u00f4ng th\u1ec3 chia m\u1ed9t s\u1ed1 cho 0!\")<br>except ValueError:<br>    print(\"L\u1ed7i: B\u1ea1n ph\u1ea3i nh\u1eadp m\u1ed9t s\u1ed1 nguy\u00ean!\")<br><\/code><\/pre>\n\n\n\n<p>C\u00e1ch ho\u1ea1t \u0111\u1ed9ng:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>N\u1ebfu ng\u01b0\u1eddi d\u00f9ng nh\u1eadp <code>0<\/code> \u2192 <code>ZeroDivisionError<\/code>.<\/li>\n\n\n\n<li>N\u1ebfu ng\u01b0\u1eddi d\u00f9ng nh\u1eadp k\u00fd t\u1ef1 thay v\u00ec s\u1ed1 \u2192 <code>ValueError<\/code>.<\/li>\n\n\n\n<li>N\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i \u2192 Ch\u01b0\u01a1ng tr\u00ecnh ch\u1ea1y b\u00ecnh th\u01b0\u1eddng.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">4. D\u00f9ng <code>else<\/code> v\u00e0 <code>finally<\/code><\/h2>\n\n\n\n<p>Ngo\u00e0i <code>try-except<\/code>, Python c\u00f2n c\u00f3 hai th\u00e0nh ph\u1ea7n \u0111\u1eb7c bi\u1ec7t:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>else<\/code>: Ch\u1ea1y khi kh\u00f4ng c\u00f3 l\u1ed7i x\u1ea3y ra.<\/li>\n\n\n\n<li><code>finally<\/code>: Lu\u00f4n ch\u1ea1y, d\u00f9 c\u00f3 l\u1ed7i hay kh\u00f4ng.<\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<br>    file = open(\"data.txt\", \"r\")  # M\u1edf file<br>    content = file.read()<br>    print(content)<br>except FileNotFoundError:<br>    print(\"L\u1ed7i: Kh\u00f4ng t\u00ecm th\u1ea5y t\u1ec7p tin!\")<br>else:<br>    print(\"T\u1ec7p \u0111\u1ecdc th\u00e0nh c\u00f4ng!\")  # Ch\u1ec9 ch\u1ea1y n\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i<br>finally:<br>    print(\"\u0110\u00f3ng t\u1ec7p tin...\")  # Lu\u00f4n ch\u1ea1y<br>    file.close()<br><\/code><\/pre>\n\n\n\n<p><code>finally<\/code> \u0111\u1ea3m b\u1ea3o t\u00e0i nguy\u00ean lu\u00f4n \u0111\u01b0\u1ee3c gi\u1ea3i ph\u00f3ng.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">5. T\u1ea1o Exception t\u00f9y ch\u1ec9nh v\u1edbi <code>raise<\/code><\/h2>\n\n\n\n<p>Python cho ph\u00e9p b\u1ea1n t\u1ea1o l\u1ed7i t\u00f9y ch\u1ec9nh b\u1eb1ng <code>raise<\/code>, gi\u00fap ki\u1ec3m so\u00e1t l\u1ed7i theo logic ri\u00eang.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5: Ki\u1ec3m tra \u0111\u1ed9 tu\u1ed5i tr\u01b0\u1edbc khi cho ph\u00e9p \u0111\u0103ng k\u00fd:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def check_age(age):<br>    if age &lt; 18:<br>        raise ValueError(\"Tu\u1ed5i ph\u1ea3i l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng 18!\")  # T\u1ea1o ngo\u1ea1i l\u1ec7 t\u00f9y ch\u1ec9nh<br>    return \"H\u1ee3p l\u1ec7!\"<br><br>try:<br>    print(check_age(15))  # Nh\u1eadp tu\u1ed5i &lt;18<br>except ValueError as e:<br>    print(f\"L\u1ed7i: {e}\")<br><\/code><\/pre>\n\n\n\n<p><code>raise<\/code> gi\u00fap ph\u00e1t hi\u1ec7n l\u1ed7i s\u1edbm v\u00e0 ng\u0103n nh\u1eadp d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">6. B\u1eaft m\u1ecdi ngo\u1ea1i l\u1ec7 b\u1eb1ng <code>Exception<\/code><\/h2>\n\n\n\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n b\u1eaft t\u1ea5t c\u1ea3 lo\u1ea1i l\u1ed7i, c\u00f3 th\u1ec3 d\u00f9ng <code>Exception<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>try:<br>    x = int(input(\"Nh\u1eadp s\u1ed1: \"))<br>    print(10 \/ x)<br>except Exception as e:<br>    print(f\"L\u1ed7i x\u1ea3y ra: {e}\")  # In ra l\u1ed7i c\u1ee5 th\u1ec3<br><\/code><\/pre>\n\n\n\n<p>Tuy nhi\u00ean, kh\u00f4ng n\u00ean l\u1ea1m d\u1ee5ng v\u00ec c\u00f3 th\u1ec3 che gi\u1ea5u l\u1ed7i quan tr\u1ecdng.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">7. T\u00f3m t\u1eaft ki\u1ebfn th\u1ee9c quan tr\u1ecdng<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng <code>try-except<\/code> \u0111\u1ec3 b\u1eaft l\u1ed7i v\u00e0 x\u1eed l\u00fd an to\u00e0n.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng nhi\u1ec1u <code>except<\/code> \u0111\u1ec3 x\u1eed l\u00fd t\u1eebng lo\u1ea1i l\u1ed7i c\u1ee5 th\u1ec3.<\/li>\n\n\n\n<li>D\u00f9ng <code>else<\/code> v\u00e0 <code>finally<\/code> \u0111\u1ec3 ki\u1ec3m so\u00e1t lu\u1ed3ng ch\u01b0\u01a1ng tr\u00ecnh t\u1ed1t h\u01a1n.<\/li>\n\n\n\n<li>D\u00f9ng <code>raise<\/code> \u0111\u1ec3 t\u1ea1o ngo\u1ea1i l\u1ec7 t\u00f9y ch\u1ec9nh, gi\u00fap ki\u1ec3m tra d\u1eef li\u1ec7u \u0111\u1ea7u v\u00e0o.<\/li>\n\n\n\n<li>B\u1eaft t\u1ea5t c\u1ea3 ngo\u1ea1i l\u1ec7 v\u1edbi <code>Exception<\/code>, nh\u01b0ng kh\u00f4ng n\u00ean l\u1ea1m d\u1ee5ng.<\/li>\n<\/ul>\n\n\n\n<p>B\u1ea1n \u0111\u00e3 t\u1eebng g\u1eb7p l\u1ed7i n\u00e0o trong Python m\u00e0 kh\u00f4ng bi\u1ebft c\u00e1ch x\u1eed l\u00fd ch\u01b0a? H\u00e3y chia s\u1ebb \u0111\u1ec3 c\u00f9ng th\u1ea3o lu\u1eadn!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong l\u1eadp tr\u00ecnh, l\u1ed7i (error) l\u00e0 \u0111i\u1ec1u kh\u00f4ng th\u1ec3 tr\u00e1nh kh\u1ecfi. N\u1ebfu kh\u00f4ng x\u1eed l\u00fd t\u1ed1t, ch\u01b0\u01a1ng tr\u00ecnh c\u00f3 th\u1ec3 b\u1ecb d\u1eebng \u0111\u1ed9t ng\u1ed9t, g\u00e2y \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}