

{"id":12634,"date":"2018-04-10T08:57:30","date_gmt":"2018-04-10T08:57:30","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=12634"},"modified":"2026-05-30T16:04:03","modified_gmt":"2026-05-30T10:34:03","slug":"checked-and-unchecked-exceptions-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/","title":{"rendered":"Difference Between Checked and Unchecked Exceptions in Java"},"content":{"rendered":"<p>Exceptions in Java is a vast concept. However, if you do not have a proper concept of how to handle exceptions, you are going to have a hard time debugging your code because the java compiler does not like uncaught exceptions.<\/p>\n<p>What are these?<\/p>\n<p>Why do we need checked and unchecked exceptions? Wouldn\u2019t the world be a better place if there were no exceptions at all?<\/p>\n<p>Let us see what we mean by checked and unchecked exceptions in Java.<\/p>\n<h3>What are Checked Exceptions in Java?<\/h3>\n<p>The JVM checks these exceptions during compile time. This is a very important point to understand.<\/p>\n<p>If you do not explicitly mention the exceptions or provide appropriate actions to execute when the exception occurs, the compiler throws an error.<\/p>\n<p>These come under compile-time errors. If you do not have prior knowledge about these, you may spend the entire day looking at your code and still not come up with a possible explanation. I am telling you this from my experience. I have had some pretty rough days.<\/p>\n<p>So simply speaking, you have to tell the compiler that a particular code will throw some exceptions, even if you are extremely sure that it won\u2019t. In order to deal with checked exceptions, you either have to mention what exception it can throw by using the \u201cthrows\u201d keyword or you need to bind it with a try-catch block.<\/p>\n<p>Let us take a simple example of a file operation in Java. However, if you are new to file operations be sure to check that article out first.<\/p>\n<p><strong>Program to illustrate Checked Exceptions in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.checkedunchecked;\r\nimport java.io.*;\r\n\r\npublic class Checked\r\n{\r\n    public static void main(String[] args)  {\r\n        \r\n        FileInputStream f = new FileInputStream(\".\/test.txt\");\/\/This results in an error.\r\n\r\n        f.close();\r\n\r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Checked.java:7: error: unreported exception FileNotFoundException; must be caught or declared to be thrown<br \/>\nFileInputStream f = new FileInputStream(&#8220;.\/test.txt&#8221;);\/\/This results in an error.<br \/>\n^<br \/>\nChecked.java:9: error: unreported exception IOException; must be caught or declared to be thrown<br \/>\nf.close();<\/div>\n<p>As you can see the compiler throws errors because I did not mention the exceptions possible in a throws clause or in a try-catch block.<\/p>\n<p>So let us do it and see if there are still errors in it.<\/p>\n<p><strong>I added a throws IOException after the main method. We will see.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.checkedunchecked;\r\nimport java.io.*;\r\n\r\npublic class Checked\r\n{\r\n    public static void main(String[] args) throws IOException {\r\n        \r\n        FileInputStream f = new FileInputStream(\".\/test.txt\");\r\n        System.out.println(\"Shraman knows how to use Checked Exceptions.\");\r\n        System.out.println(\"He also created a file called test.txt in this directory\");\r\n        f.close();\r\n        \r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Shraman knows how to use Checked Exceptions.<br \/>\nHe also created a file called test.txt in this directory<\/div>\n<p>However, you might be confused as to why we are only mentioning the IOException class after the throws keyword.<\/p>\n<p>This is because IOException is a superclass of the FileNotFound Exception.<\/p>\n<p>So the compiler understands any exceptions which are a subclass are automatically taken care of.<\/p>\n<p>We can also use try-catch blocks instead of the throws keyword. That is another method of handling checked exceptions.<\/p>\n<p>Let us see.<\/p>\n<p><strong>Program to illustrate handling checked exceptions with try-catch blocks in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.checkedunchecked;\r\n\r\nimport java.io.*;\r\n\r\npublic class Checked\r\n{\r\n    public static void main(String[] args)  {\r\n        try {\r\n\r\n        FileInputStream f = new FileInputStream(\".\/test.txt\");\r\n        System.out.println(\"Shraman knows how to use Checked Exceptions.\");\r\n        System.out.println(\"He also created a file called test.txt in this directory\");\r\n        f.close();\r\n        \r\n\r\n            \r\n        } catch (Exception e) {\r\n            \/\/TODO: handle exception\r\n            System.out.println(\"There has been some sort of a problem! \");\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Shraman knows how to use Checked Exceptions.<br \/>\nHe also created a file called test.txt in this directory<\/div>\n<p>As you can see in the program above, I did not mention the exceptions in the throws clause. I used a try-catch block instead.<\/p>\n<p>This is another way to work with checked exceptions in Java.<\/p>\n<h3>What are Unchecked Exceptions in Java?<\/h3>\n<p>The compiler, unlike in Checked Exceptions, does not check for unchecked exceptions in the program.<\/p>\n<p>It does not throw errors if you do not mention the exception using the throws keyword. It does not even give you compilation errors if you do not bind the code with a try-catch block.<\/p>\n<p>These exceptions generally rise due to the poor input and data handling of a program.<\/p>\n<p>One very popular exception is the ArithmeticException which arises if you divide a number by 0.<\/p>\n<p>Let us see what it actually is with an example<\/p>\n<p><strong>Java program to illustrate the use of Unchecked Exceptions in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.checkedunchecked;\r\nimport java.util.Scanner;\r\n\r\npublic class Unchecked {\r\n    public static void main(String[] args) {\r\n        \r\n        System.out.println(\"Let us learn about unchecked exceptions in Java\");\r\n        int a,b;\r\n        Scanner sc =new Scanner(System.in);\r\n        System.out.println(\"Please enter the first number\");\r\n        a=sc.nextInt();\r\n        System.out.println(\"Please enter the second number\");\r\n        b=sc.nextInt();\r\n        int c=a\/b;\r\n        System.out.println(\"The quotient is \"+c);\r\n\r\n        \r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Let us learn about unchecked exceptions in Java<br \/>\nPlease enter the first number<br \/>\n14<br \/>\nPlease enter the second number<br \/>\n0<br \/>\nException in thread &#8220;main&#8221; java.lang.ArithmeticException: \/ by zero<br \/>\nat Unchecked.main(Unchecked.java:13)<\/div>\n<p>If you observe in the above example, we did not explicitly use the throws keyword or a try-catch block, but the program still compiles like a charm.<\/p>\n<p>However, when we ask the program to divide 15 with 0 it goes haywire. You can\u2019t complain, because you are dividing the number by 0. What did you expect?<\/p>\n<p>These are unchecked exceptions in Java.<\/p>\n<p>However, we as developers must also check these exceptions even when these are not possible for the compile to check,<\/p>\n<p>These error messages may cause the application to crash instead of notifying the user that their input is wrong.<\/p>\n<p>So we should use try-catch blocks to properly handle the exception and provide appropriate output to the user.<\/p>\n<p><strong>We can change the above program to:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.checkedunchecked;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class Unchecked {\r\n    public static void main(String[] args) {\r\n        try {\r\n\r\n        System.out.println(\"Let us learn about unchecked exceptions in Java\");\r\n        int a,b;\r\n        Scanner sc =new Scanner(System.in);\r\n        System.out.println(\"Please enter the first number\");\r\n        a=sc.nextInt();\r\n        System.out.println(\"Please enter the second number\");\r\n        b=sc.nextInt();\r\n        int c=a\/b;\r\n        System.out.println(\"The quotient is \"+c);\r\n\r\n            \r\n        } catch (Exception e) {\r\n            \/\/TODO: handle exception\r\n            System.out.println(\"Java faced \\'\"+ e.getMessage()+\" \\' exception.\");\r\n            System.out.println(\"Java thinks you entered a wrong input there. Please check and re-run the program.\");\r\n        }\r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Let us learn about unchecked exceptions in Java<br \/>\nPlease enter the first number<br \/>\n13<br \/>\nPlease enter the second number<br \/>\n0<br \/>\nJava faced &#8216;\/ by zero &#8216; exception.<br \/>\nJava thinks you entered a wrong input there. Please check and re-run the program.<\/div>\n<div><\/div>\n<div><strong>Way to resolve checked and unchecked exception<\/strong><\/div>\n<p>Checked exceptions are handled using the try and catch block. The unchecked exceptions are solved by doing the changes in the program explicitly. The compiler does not evaluate the checked exception.<\/p>\n<h3>Summary<\/h3>\n<p>At last, we are at the end of our article, checked and unchecked exceptions in Java.<\/p>\n<p>We learned about what checked exceptions are, and the way we must use them in the program.<\/p>\n<p>We also learned about unchecked exceptions and how they are necessary to be properly handled by the program to facilitate smooth execution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exceptions in Java is a vast concept. However, if you do not have a proper concept of how to handle exceptions, you are going to have a hard time debugging your code because the&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84906,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[3225,4254,4427,4431,7732,8326,15028,15233],"class_list":["post-12634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-custom-exception-in-java","tag-example-of-checked-exceptions-in-java","tag-exception-class-in-java","tag-exception-handling-in-java-with-examples","tag-java-unchecked-exceptions","tag-list-of-checked-and-unchecked-exceptions-in-java","tag-types-of-checked-exceptions-in-java","tag-user-defined-exceptions-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Difference Between Checked and Unchecked Exceptions in Java - DataFlair<\/title>\n<meta name=\"description\" content=\"Difference Between Checked and Unchecked Exceptions in Java-Checked exception in java &amp; java unchecked exception with example.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference Between Checked and Unchecked Exceptions in Java - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Difference Between Checked and Unchecked Exceptions in Java-Checked exception in java &amp; java unchecked exception with example.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-10T08:57:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-30T10:34:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference Between Checked and Unchecked Exceptions in Java - DataFlair","description":"Difference Between Checked and Unchecked Exceptions in Java-Checked exception in java & java unchecked exception with example.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Difference Between Checked and Unchecked Exceptions in Java - DataFlair","og_description":"Difference Between Checked and Unchecked Exceptions in Java-Checked exception in java & java unchecked exception with example.","og_url":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-10T08:57:30+00:00","article_modified_time":"2026-05-30T10:34:03+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Difference Between Checked and Unchecked Exceptions in Java","datePublished":"2018-04-10T08:57:30+00:00","dateModified":"2026-05-30T10:34:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/"},"wordCount":915,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg","keywords":["custom exception in java","Example of Checked Exceptions in Java","exception class in java","exception handling in java with examples","Java Unchecked Exceptions","list of checked and unchecked exceptions in java","types of checked exceptions in java","User-Defined Exceptions in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/","url":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/","name":"Difference Between Checked and Unchecked Exceptions in Java - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg","datePublished":"2018-04-10T08:57:30+00:00","dateModified":"2026-05-30T10:34:03+00:00","description":"Difference Between Checked and Unchecked Exceptions in Java-Checked exception in java & java unchecked exception with example.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Checked-and-Unchecked-Exceptions-in-Java.jpg","width":1200,"height":628,"caption":"Checked and Unchecked Exceptions in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/checked-and-unchecked-exceptions-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Difference Between Checked and Unchecked Exceptions in Java"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=12634"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12634\/revisions"}],"predecessor-version":[{"id":148528,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12634\/revisions\/148528"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84906"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=12634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=12634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=12634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}