{"id":16851,"date":"2023-11-08T09:59:48","date_gmt":"2023-11-08T04:29:48","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/"},"modified":"2025-01-06T19:39:48","modified_gmt":"2025-01-06T14:09:48","slug":"exception-handling-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/","title":{"rendered":"Exception Handling in Java with Examples"},"content":{"rendered":"\n<p>Exception handling in java is one of the powerful mechanisms to handle runtime errors caused by exceptions. Exception handling plays an important role in software development. This article helps you understand java exception, exception in java, java exception handling, java exception hierarchy, types of exception in java, and many more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-exception-handling-in-java\"><strong><strong>What is Exception Handling in Java?<\/strong><\/strong><\/h2>\n\n\n\n<p>Exception handling in java helps in minimizing exceptions and helps in recovering from exceptions. It is one of the powerful mechanisms to handle runtime exceptions and makes it bug-free. Exception handling helps in maintaining the flow of the program. An exception handling is defined as an abnormal condition that may happen at runtime and disturb the normal flow of the program.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p><strong>Also Read: <a rel=\"noreferrer noopener\" aria-label=\"Java Tutorial for beginners (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/blog\/java-tutorial-for-beginners\/\" target=\"_blank\">Java Tutorial for beginners<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-exception\"><strong>What is an Exception?<\/strong><\/h2>\n\n\n\n<p>An expectation is an unexpected&nbsp;event that occurs while executing the program, that disturbs the normal flow of the code. <\/p>\n\n\n\n<p><strong>Exception handling in java with an example:<\/strong><\/p>\n\n\n\n<p>Let's say,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>statement\nstatement\nstatement\nexception \u2026\u2026\u2026\u2026 an exception occurred, then JVM will handle it and will exit the prog.\nstatement\nstatement\nstatement<\/code><\/pre>\n\n\n\n<p>For handling exceptions, there are<strong> 2 possible approaches<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-jvm\"><strong>1. JVM<\/strong><\/h3>\n\n\n\n<p>If an exception is not handled explicitly, then JVM takes the responsibility of handling the exception.<\/p>\n\n\n\n<p>Once the exception is handled, JVM will halt the program and no more execution of code will take place<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example<\/strong>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\nclass Main {\n    public static void main (String&#091;] args) {\n        System.out.println(5\/0);\n        System.out.println(\"End of program!\");\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Runtime Error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> Exception in thread \"main\" java.lang.ArithmeticException: \/ by zero\n at Main.main(File.java:5) <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-developer\"><strong>2<\/strong>. <strong>Developer<\/strong><\/h3>\n\n\n\n<p>Developers can explicitly write the implementation for handling the exception. Once an exception is handled, the normal execution of code will continue.<\/p>\n\n\n\n<p><strong>Preferable<\/strong>: handle exceptions to ensure your code gets executed normally.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-exception-hierarchy\"><strong>Java Exception Hierarchy<\/strong><\/h2>\n\n\n\n<p><strong>Exception Hierarchy - <\/strong>Following is the Exception Handling in Java handling hierarchy.<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Throwable <\/strong>-\n<ul class=\"wp-block-list\">\n<li>It is the root class for the exception hierarchy in java.&nbsp;<\/li>\n\n\n\n<li>It is in the java.lang package.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Error<\/strong> -\n<ul class=\"wp-block-list\">\n<li>Subclass of Throwable.<\/li>\n\n\n\n<li>Consist of abnormal condition that is out of one's control and depends on the environment<\/li>\n\n\n\n<li>They can't be handled and will always result in the halting of the program.<\/li>\n\n\n\n<li>Eg: StackOverFlowError that can happen in infinite loop or recursion<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Exception <\/strong>-\n<ul class=\"wp-block-list\">\n<li>Subclass of Throwable.<\/li>\n\n\n\n<li>Consist of abnormal conditions that can be handled explicitly.<\/li>\n\n\n\n<li>If one handles the exception then our code will continue to execute smoothly.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-exception-in-java\"><strong>Types of exception in Java<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Checked Exceptions<\/strong><ul><li>Those exceptions that are checked at compile-time comprises checked exceptions.<\/li><\/ul><ul><li>They are child classes of Exception except for RuntimeException.<\/li><\/ul><ul><li>The program will not compile if they are not handled.<\/li><\/ul><ul><li>Example: IOException, ClassNotFoundException, etc.<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Unchecked Exceptions<\/strong><ul><li>Those exceptions that are checked at runtime comprises unchecked exceptions.<\/li><\/ul><ul><li>They are child classes of RuntimeException.<\/li><\/ul><ul><li>They give runtime errors if not handled explicitly.<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>Example: ArithmeticException, NullPointerException etc.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-checked-and-unchecked-exception\"><strong>Difference between Checked and Unchecked Exception<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Checked Exceptions<\/strong><\/td><td><strong>Unchecked Exceptions<\/strong><\/td><\/tr><tr><td>Occur at compile time.<\/td><td>Occur at runtime.<\/td><\/tr><tr><td>The compiler checks for a checked exception.<\/td><td>The compiler doesn\u2019t check for exceptions.<\/td><\/tr><tr><td>Can be handled at the compilation time.<\/td><td>Can\u2019t be caught or handled during compilation time.<\/td><\/tr><tr><td>The JVM requires that the exception be caught and handled.<\/td><td>The JVM doesn\u2019t require the exception to be caught and handled.<\/td><\/tr><tr><td>Example of Checked exception- \u2018File Not Found Exception\u2019<\/td><td>Example of Unchecked Exceptions- \u2018No Such Element Exception\u2019<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-exception-index\"><strong>Java Exception Index<\/strong><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-exception-keywords\"><strong>Java Exception Keywords<\/strong><\/h2>\n\n\n\n<p>Exception Handling in java is managed via five keywords: try, catch, throw, throws, and finally. Here are 5 keywords that are used in handling exceptions in Java<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Keyword<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>try<\/td><td>This keyword is used to specify a block and this block must be followed by either catch or finally. That is, we can't use try block alone.<\/td><\/tr><tr><td>catch<\/td><td>This keyword must be preceded by a try block to handle the exception and can be followed by a final block later.<\/td><\/tr><tr><td>finally<\/td><td>This keyword is used to execute the program, whether an exception is handled or not.<\/td><\/tr><tr><td>throw<\/td><td>This keyword is used to throw an exception.<\/td><\/tr><tr><td>throws<\/td><td>This keyword is used to declare exceptions.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-try-catch-block\"><strong>Java Try-Catch Block<\/strong><\/h2>\n\n\n\n<p>Try-catch syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try{\n}\ncatch(Exception e){\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Try-catch Example:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionDemo {\n\tpublic static void main (String&#091;] args) {\n\t\tint a=10;\n\t\tfor(int i=3;i&gt;=0;i--)\n\t\t   try{\n\t\t     System.out.println(a\/i);  \n\t\t   }catch(ArithmeticException e){\n\t\t       System.out.println(e);\n\t\t   }\n\t}\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\">Output:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">3\n5\n10\njava.lang.ArithmeticException: \/ by zero <\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>try <\/strong>block contains the code that might throw an exception. Don't write anything extra in try as statements after the exception will not get executed if the exception occurred. Try must be immediately followed by catch or finally block.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionDemo {\n\tpublic static void main (String&#091;] args) {\n\t\tint a=10;\n\t\tfor(int i=3;i&gt;=0;i--)\n\t\t   try{\n\t\t     System.out.println(a\/i);  \n\t\t   }\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Compile-time error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">prog.java:5: error: 'try' without 'catch', 'finally' or resource declarations\n  &nbsp; try{\n  &nbsp; ^\n1 error <\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The catch block is used to catch the exception thrown by statements in the try block. The catch must follow try else it will give a compile-time error.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionDemo {\n\tpublic static void main (String&#091;] args) {\n\t\tint a=10;\n\t\tfor(int i=3;i&gt;=0;i--)\n\t\t   try{\n\t\t     System.out.println(a\/i);  \n\t\t   }\n\t\t   System.out.println(\"between try and catch\");\n\t\t   catch(ArithmeticException e){\n\t\t       System.out.println(e);\n\t\t   }\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"compile-time-error\"><strong>Compile Time Error:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">prog.java:5: error: 'try' without 'catch', 'finally' or resource declarations\n  &nbsp; try{\n  &nbsp; ^\nprog.java:9: error: 'catch' without 'try'\n  &nbsp; catch(ArithmeticException e){\n  &nbsp; ^\n2 errors <\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"things-to-remember\"><strong>Things to Remember:<\/strong><\/h4>\n\n\n\n<p>Do not keep any code after the statement which is prone to exception. Because if an exception occurred, it will straight away jump to the catch or finally block, ignoring all other statements in the try block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n         try\n       {\n             System.out.println(4\/0);\n\t \/\/will not get printed\n             System.out.println(\"end of try!\");\n        }\ncatch(ArithmeticException e)\n        {\n            System.out.println(\"divide by 0\");\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\">Output:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">divide by 0<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>While catching the exception in the catch block, either you can have directly the class of exception or its superclass.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example: Exact Exception<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n           }\n      \n        \/\/ArithmeticException \n        catch(ArithmeticException e){\n            System.out.println(\"divide by 0\");\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">divide by 0<\/pre>\n\n\n\n<p><strong>Example: Superclass of Exact Exception<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n           }\n      \n        \/\/superclass of ArithmeticException \n        catch(Exception e){\n            System.out.println(\"divide by 0\");\n        }\n     }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">divide by 0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-multiple-catch-block\"><strong>Java Multiple Catch Block<\/strong><\/h2>\n\n\n\n<p>If you have multiple catches, you have to maintain the hierarchy from subclass to superclass.<\/p>\n\n\n\n<p><strong>Incorrect:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n        }catch(Exception e)\n        {\n            System.out.println(\"Exception : divide by 0\");\n        }catch(ArithmeticException e)\n        {\n            System.out.println(\"ArithmeticException :divide by 0\");\n        }\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Compile-time error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> prog.java:11: error: exception ArithmeticException has already been caught\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}catch(ArithmeticException e)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^\n1 error <\/pre>\n\n\n\n<p><strong>Correct:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n        }catch(ArithmeticException e)\n        {\n            System.out.println(\"ArithmeticException : divide by 0\");\n        }catch(Exception e)\n        {\n            System.out.println(\"Exception : divide by 0\");\n        }\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArithmeticException: Divide by 0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-nested-try\"><strong>Java Nested Try<\/strong><\/h2>\n\n\n\n<p>When there is another try block within the try block: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n                try{\n                    int&#091;] a={1,2,3};\n                    System.out.println(a&#091;3]);\n                }\n   catch(ArrayIndexOutOfBoundsException e)\n                {\n                    System.out.println(\"Out of bounds\");\n                }\n              System.out.println(4\/0);\n        }\n       catch(ArithmeticException e)\n        {\n            System.out.println(\"ArithmeticException : divide by 0\");\n        }\n\t}\n    }\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Out of bounds\nArithmeticException: Divide by 0 <\/pre>\n\n\n\n<p><strong>Note - If we put code of outer try before inner try, then if an exception occurred, it will ignore the entire inner try and move directly to its catch block.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n               System.out.println(4\/0);\n               try{\n                    int&#091;] a={1,2,3};\n                    System.out.println(a&#091;3]);\n                }\n   catch(ArrayIndexOutOfBoundsException e)\n                {\n                    System.out.println(\"Out of bounds\");\n                }\n        }\n       catch(ArithmeticException e)\n        {\n            System.out.println(\"ArithmeticException : divide by 0\");\n        }\n\t}\n    }\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArithmeticException: Divide by 0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-finally-block\"><strong>Java Finally Block<\/strong><\/h2>\n\n\n\n<p>Contains code that must be executed no matter if an exception is thrown or not. It contains code of file release, closing connections, etc.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n        }catch(Exception e)\n        {\n            System.out.println(e);       \n        }\n        finally\n        {\n            System.out.println(\"finally executed\");\n        }\n        \n       \t        System.out.println(\"end\");\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">java.lang.ArithmeticException: \/ by zero\nfinally executed\nend <\/pre>\n\n\n\n<p>Finally, will execute even when we do not handle exceptions. Before halting the program, JVM checks if there is a \"finally\" block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\tpublic static void main (String&#091;] args) {\n        try{\n            System.out.println(4\/0);\n            \n        }finally\n        {\n            System.out.println(\"cleaning.......\");\n        }\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<p><strong>Runtime Error:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> Exception in thread \"main\" java.lang.ArithmeticException: \/ by zero\n at Main.main(File.java:4) <\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cleaning.......<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-final-vs-finally-vs-finalize\"><strong>Java Final vs Finally vs Finalize<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Final<\/strong><\/td><td><strong>Finally<\/strong><\/td><td><strong>Finalize<\/strong><\/td><\/tr><tr><td>Final is used to apply restrictions on class, method, and variable<\/td><td>Finally is used in coding, it will be executed whether an exception is handled or not.<\/td><td>Finalize is used to perform clean-up processing before garbage is collected.<\/td><\/tr><tr><td>Final is a keyword in java<\/td><td>Finally is a block in java<\/td><td>Finalize is a method in java<\/td><\/tr><tr><td>Final is executed upon its call.<\/td><td>Finally executes after\"try-catch\" block.<\/td><td>finalize executes just before the destruction of the object.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-throw-keyword\"><strong>Java Throw Keyword<\/strong><\/h2>\n\n\n\n<p>It is a keyword that is used to explicitly throw an exception.<\/p>\n\n\n\n<p>We can use throw where according to our logic an exception should occur. <\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionDemo {\n\tstatic void canVote(int age){\n\t\tif(age&lt;18)\n            try{\n                throw new Exception();\n            }catch(Exception e){\n                System.out.println(\"you are not an adult!\");\n            }\n\t\telse\n\t\t   System.out.println(\"you can vote!\");\n\t}\n\tpublic static void main (String&#091;] args) {\n\t\tcanVote(20);\n\t\tcanVote(10);\n\t}\n}\n\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">you can vote!\nyou are not an adult! <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-throws-keyword\"><strong>Java Throws Keyword<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Throws keyword is used when callee doesn't want to handle the exception rather it wants to extend this responsibility of handling the exception to the caller of the function.<\/li>\n\n\n\n<li>Basically says what sort of exception the code can throw and relies on the caller to handle it.<\/li>\n\n\n\n<li>It is used to handle checked Exceptions as the compiler will not allow code to compile until they are handled.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExceptionDemo {\n\tstatic void func(int a) throws Exception{\n\t\t   System.out.println(10\/a);  \n\t}\n\tpublic static void main (String&#091;] args) {\n\t\ttry{\n\t\t    func(10);\n\t\t    func(0);\n\t\t}catch(Exception e){\n\t\t   System.out.println(\"can't divide by zero\");\n\t\t}\n\t\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\ncan't divide by zero <\/pre>\n\n\n\n<p>If callee can throw multiple exceptions, then all will be thrown simultaneously.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class ExceptionDemo {\n\tstatic void func(int a,int b) throws ArithmeticException, ArrayIndexOutOfBoundsException{\n\t\t   System.out.println(10\/a); \n\t\t   int&#091;] arr={1,2,3};\n\t\t   System.out.println(arr&#091;b]);\n\t}\n\tpublic static void main (String&#091;] args) {\n\t\tScanner in=new Scanner(System.in);\n\t\tfor(int i=0;i&lt;3;i++){\n\t\ttry{\n\t\t    func(in.nextInt(),in.nextInt());\n    \t\t}catch(ArithmeticException e){\n    \t\t   System.out.println(\"can't divide by zero\");\n    \t\t}catch(ArrayIndexOutOfBoundsException e){\n    \t\t   System.out.println(\"Out of bounds!\");\n    \t\t}\n\t\t     }\n\t\t\n\t}\n   }\n<\/code><\/pre>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2 1\n0 1\n2 3 <\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5\n2\ncan't divide by zero\n5\nOut of bounds! <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-throw-vs-throws\"><strong>Java Throw vs Throws<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Throw<\/strong><\/td><td><strong>Throws<\/strong><\/td><\/tr><tr><td>This keyword is used to explicitly throw an exception.<\/td><td>This keyword is used to declare an exception.<\/td><\/tr><tr><td>A checked exception cannot be propagated with throw only.<\/td><td>A checked exception can be propagated with throws.<\/td><\/tr><tr><td>The throw is followed by an instance and used with a method<\/td><td>Throws are followed by class and used with the method signature.<\/td><\/tr><tr><td>You cannot throw multiple exceptions.<\/td><td>You can declare multiple exceptions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"java-custom-exception\"><strong>Java Custom Exception<\/strong><\/h2>\n\n\n\n<p>You can create your own exception and give implementation as to how it should behave. Your exception will behave like a child's class of Exception.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"syntax\"><strong>Syntax<\/strong>:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n class YourException extends Exception{}\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example:<\/strong>\n<ul class=\"wp-block-list\">\n<li>let's say, you are working with an airline company&nbsp;<\/li>\n\n\n\n<li>You are in the luggage check-in department and as per rules, you can allow 15kg per customer.<\/li>\n\n\n\n<li>So now more than 15kg of weight is an abnormal condition for us or in other words its an exception<\/li>\n\n\n\n<li>This is our logic-based exception, so we'll create our custom exception WeightLimitExceeded&nbsp;<\/li>\n\n\n\n<li>As per syntax, it will extend Exception.<\/li>\n\n\n\n<li>We define the constructor which will get invoked as soon as an exception will be thrown<\/li>\n\n\n\n<li>We have to explicitly throw the exception and hence we will use throw keyword for that.<\/li>\n\n\n\n<li>Using throws keyword is as per our need. If we are handling an exception where it is getting thrown then we can avoid throws, else we will use throws and handle it in the caller.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Implementation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\nclass WeightLimitExceeded extends Exception{\n    WeightLimitExceeded(int x){\n        System.out.print(Math.abs(15-x)+\" kg : \");\n    }\n}\n\n\nclass Main {\n    void validWeight(int weight) throws WeightLimitExceeded{\n        if(weight&gt;15)\n            throw new WeightLimitExceeded(weight);\n        else\n            System.out.println(\"You are ready to fly!\");\n    }\n    \n      public static void main (String&#091;] args) {\n        Main ob=new Main();\n        Scanner in=new Scanner(System.in);\n        for(int i=0;i&lt;2;i++){\n            try{\n                ob.validWeight(in.nextInt());\n            }catch(WeightLimitExceeded e){\n                System.out.println(e);\n            }\n        }\n        \n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Input<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">20\n7 <\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5 kg : WeightLimitExceeded\nYou are ready to fly! <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"exception-handling-in-java-with-method-overriding\"><strong>Exception Handling in java with method overriding<\/strong><\/h2>\n\n\n\n<p>Exception Handling in Java with Method Overriding is an<strong> <\/strong>overridden method that declares to throw an exception and declare that it can throw the same exception or subtype of that exception.<\/p>\n\n\n\n<p>To handle the exception in Java, you will have to follow three important rules. They are depicted in the below figure.<\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13.png\"><img decoding=\"async\" width=\"1024\" height=\"697\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-1024x697.png\" alt=\"exception handling in java\" class=\"wp-image-39194\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-1024x697.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-300x204.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-768x522.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-696x473.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-617x420.png 617w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13-150x102.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/07\/java13.png 1051w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Exception Handling in Java with Method Overriding<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-and-disadvantages-of-exception-handling-in-java\"><strong>Advantages and disadvantages of exception handling in java <\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"advantages-of-excepting-handling-in-java\"><strong>Advantages of excepting handling in java&nbsp;<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Separating Error-Handling Code from \"Regular\" Code<\/li>\n\n\n\n<li>Propagating Errors Up the Call Stack<\/li>\n\n\n\n<li>Grouping and Differentiating Error Types<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"disadvantages-of-excepting-handling-in-java\"><strong>Disadvantages of excepting handling in java<\/strong>&nbsp;<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Experiencing unnecessary overhead<\/li>\n\n\n\n<li>Not understanding how the application really works<\/li>\n\n\n\n<li>Filling your logs with noisy events<\/li>\n\n\n\n<li>Inability to focus on what actually matters<\/li>\n<\/ul>\n\n\n\n<p>This brings us to the end of this article on exception handling in java. We hope that you are now clear about the concept of exception handling in java. If you wish to know more about the java programming language, then log on to our free <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-programming\" target=\"_blank\" rel=\"noreferrer noopener\">java online course with certificate<\/a> and power ahead in your career.<\/p>\n\n\n\n<p><strong>Also Watch: <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Exception Handling in Java in Hindi | Java Programming | Java Tutorial | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/tsEfe7kFvlM?start=3&feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Engaging in the study of Java programming suggests a keen interest in the realm of software development. For those embarking upon this journey with aspirations towards a career in this field, it is recommended to explore the following pages in order to acquire a comprehensive understanding of the development career path:<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-cyan-bluish-gray-background-color has-background\"><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/fees\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses fees<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/software-engineering\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Software engineering courses eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Exception handling in java is one of the powerful mechanisms to handle runtime errors caused by exceptions. Exception handling plays an important role in software development. This article helps you understand java exception, exception in java, java exception handling, java exception hierarchy, types of exception in java, and many more. What is Exception Handling in [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":17232,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-16851","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Exception Handling in Java with Examples<\/title>\n<meta name=\"description\" content=\"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exception Handling in Java with Examples\" \/>\n<meta property=\"og:description\" content=\"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-08T04:29:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-06T14:09:48+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Exception Handling in Java with Examples\",\"datePublished\":\"2023-11-08T04:29:48+00:00\",\"dateModified\":\"2025-01-06T14:09:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/\"},\"wordCount\":1620,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/BLOG-Images_7-7-2020-08.png\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/\",\"name\":\"Exception Handling in Java with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/BLOG-Images_7-7-2020-08.png\",\"datePublished\":\"2023-11-08T04:29:48+00:00\",\"dateModified\":\"2025-01-06T14:09:48+00:00\",\"description\":\"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/BLOG-Images_7-7-2020-08.png\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/BLOG-Images_7-7-2020-08.png\",\"width\":1000,\"height\":700,\"caption\":\"exception handling in java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/exception-handling-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Exception Handling in Java with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Exception Handling in Java with Examples","description":"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.","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:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Exception Handling in Java with Examples","og_description":"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-11-08T04:29:48+00:00","article_modified_time":"2025-01-06T14:09:48+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png","type":"image\/png"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Exception Handling in Java with Examples","datePublished":"2023-11-08T04:29:48+00:00","dateModified":"2025-01-06T14:09:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/"},"wordCount":1620,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/","name":"Exception Handling in Java with Examples","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png","datePublished":"2023-11-08T04:29:48+00:00","dateModified":"2025-01-06T14:09:48+00:00","description":"Exception handling in java is one of the powerful mechanisms to handle runtime errors. It plays an important role in Software Development.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png","width":1000,"height":700,"caption":"exception handling in java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/exception-handling-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Exception Handling in Java with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08-150x150.png",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08-300x210.png",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08-768x538.png",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/BLOG-Images_7-7-2020-08.png",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":1,"uagb_excerpt":"Exception handling in java is one of the powerful mechanisms to handle runtime errors caused by exceptions. Exception handling plays an important role in software development. This article helps you understand java exception, exception in java, java exception handling, java exception hierarchy, types of exception in java, and many more. What is Exception Handling in&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=16851"}],"version-history":[{"count":70,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16851\/revisions"}],"predecessor-version":[{"id":114884,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16851\/revisions\/114884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/17232"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=16851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=16851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=16851"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=16851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}