{"id":99,"date":"2025-08-17T02:01:27","date_gmt":"2025-08-17T02:01:27","guid":{"rendered":"https:\/\/learn.rantissi.my.id\/?p=99"},"modified":"2025-08-17T02:01:28","modified_gmt":"2025-08-17T02:01:28","slug":"input-validasi-error-handling-try-except","status":"publish","type":"post","link":"https:\/\/learn.rantissi.my.id\/index.php\/2025\/08\/17\/input-validasi-error-handling-try-except\/","title":{"rendered":"Input Validasi &amp; Error Handling (try \u2013 except)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"mengapa-butuh-try-except\">Mengapa Butuh&nbsp;<code>try-except<\/code>?<\/h2>\n\n\n\n<p>Tanpa penanganan error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>angka = int(input(\"Masukkan angka: \"))\nprint(angka * 2)\n<\/code><\/pre>\n\n\n\n<p>Kalau user salah ketik (misal:&nbsp;<code>abc<\/code>), program langsung error:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>ValueError: invalid literal for int() with base 10: 'abc'<\/code><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"solusinya-gunakan-try-except\">Solusinya: Gunakan&nbsp;<code>try<\/code>&nbsp;\u2013&nbsp;<code>except<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    angka = int(input(\"Masukkan angka: \"))\n    print(\"Hasil kali 2:\", angka * 2)\nexcept ValueError:\n    print(\"Input harus berupa angka!\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"gunakan-juga-while-agar-minta-ulang-sampai-valid\">Gunakan juga&nbsp;<code>while<\/code>&nbsp;agar minta ulang sampai valid:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:\n    try:\n        umur = int(input(\"Masukkan umur kamu: \"))\n        break\n    except ValueError:\n        print(\"Harus angka ya, coba lagi.\")\n\nprint(\"Umur kamu adalah:\", umur)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mengapa Butuh&nbsp;try-except? Tanpa penanganan error: Kalau user salah ketik (misal:&nbsp;abc), program langsung error: ValueError: invalid literal for int() with base 10: &#8216;abc&#8217; Solusinya: Gunakan&nbsp;try&nbsp;\u2013&nbsp;except Gunakan juga&nbsp;while&nbsp;agar minta ulang sampai valid:<\/p>\n","protected":false},"author":1,"featured_media":20,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":1,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions\/100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media\/20"}],"wp:attachment":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}