Core JAVA

Exception Handling and Checked and Unchecked Exception

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><b>Exceptions in Java &&num;8211&semi; What are Java Exceptions&quest;<&sol;b><br &sol;>&NewLine;A Java Exception is an abnormal condition that arises during the execution of a program and also called a run-time error&period; An exception in Java signals the unusual or catastrophic situations that can arise&period;<br &sol;>&NewLine;<b>Examples of Java Exceptions are&colon;<&sol;b><&sol;p>&NewLine;<ul>&NewLine;<li>Incorrect user input such as division by zero<&sol;li>&NewLine;<li>File that needs to be opened not found<&sol;li>&NewLine;<li>Network connection problem<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b> Types of Java Exceptions<&sol;b><br &sol;>&NewLine;Like anything else in this world&comma; Exceptions can also be categorized&excl; Based on whether or not you are required to handle the exception&comma; there are two types of exceptions&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Checked Exceptions<&sol;li>&NewLine;<li>Unchecked Exceptions&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>Checked Exceptions&colon; <&sol;b>These are the type of exceptions for which the compiler checks to ensure that your code is prepared for handling such exceptions&period; These are the exceptions that you can expect to occur frequently and you must handle them in your code&period; The conditions that generate such exceptions are generally outside the control of your program and they can occur in a correct program&period; But you can anticipate them and thus you must write code to deal with them&period; Programmatically&comma; checked exceptions are the instances of the Exception class or one of its subclasses&comma; excluding <i><b>RuntimeException <&sol;b><&sol;i>subtree&period; <&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;ffb62a&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24px&semi; margin&colon; 0px&semi; orphans&colon; auto&semi; padding&colon; 0px&semi; text-align&colon; left&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;"><b>List of checked exception<&sol;b><&sol;div>&NewLine;<ul style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;ffb62a&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24px&semi; orphans&colon; auto&semi; text-align&colon; left&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">&NewLine;<li><i><b>ClassNotFoundException <&sol;b><&sol;i>Class not found&period;<&sol;li>&NewLine;<li><i><b>CloneNotSupportedException <&sol;b><&sol;i>Attempt to clone an object that does not implement the<u>Cloneable<&sol;u>&nbsp&semi;interface&period;<&sol;li>&NewLine;<li><i><b>IllegalAccessException <&sol;b><&sol;i>Access to a class is denied&period;<&sol;li>&NewLine;<li><i><b>InstantiationException <&sol;b><&sol;i>Attempt to create an object of an abstract class or interface&period;<&sol;li>&NewLine;<li><i><b>InterruptedException <&sol;b><&sol;i>One thread has been interrupted by another thread&period;<&sol;li>&NewLine;<li><i><b>NoSuchFieldException <&sol;b><&sol;i>A requested field does not exist&period;<&sol;li>&NewLine;<li><i><b>NoSuchMethodException <&sol;b><&sol;i>A requested method does not exist&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Unchecked Exceptions&colon; <&sol;b>The compiler does not check for such type of exceptions&period; Unchecked Exceptions comprise of run time exceptions &lpar;of type <i><b>RuntimeException <&sol;b><&sol;i>or its subclasses&rpar; and errors &lpar;of type Error or its subclasses&rpar;&period; Runtime Exceptions occur due to program bugs and include exceptions such as division by zero and invalid array indexing&period; You can not recover from an unchecked exception and you are not required to handle such type of exceptions either&comma; but still you can do so if you want&period; <b><i>ArithmeticException <&sol;i><&sol;b>class is an example of unchecked exception&period; Errors are the exceptions that are not expected to be caught under normal circumstances by your program&period; They are used by the Java run time environment to indicate errors related to run time environment itself&period; Stack Overflow is an example of error&period;<&sol;p>&NewLine;<div style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;ffb62a&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24px&semi; margin&colon; 0px&semi; orphans&colon; auto&semi; padding&colon; 0px&semi; text-align&colon; left&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;"><b>List of unchecked exception<&sol;b><&sol;div>&NewLine;<ul style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;ffb62a&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; 24px&semi; orphans&colon; auto&semi; text-align&colon; left&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">&NewLine;<li><i><b>ArithmeticException <&sol;b><&sol;i>Arithmetic error&comma; such as divide-by-zero&period;<&sol;li>&NewLine;<li><i><b>ArrayIndexOutOfBoundsException <&sol;b><&sol;i>Array index is out-of-bounds&period;<&sol;li>&NewLine;<li><i><b>ArrayStoreException <&sol;b><&sol;i>Assignment to an array element of an incompatible type&period;<&sol;li>&NewLine;<li><i><b>ClassCastException <&sol;b><&sol;i>Invalid cast&period;<&sol;li>&NewLine;<li><b><i>IllegalArgumentException <&sol;i><&sol;b>Illegal argument used to invoke a method&period;<&sol;li>&NewLine;<li><i><b>IllegalMonitorStateException <&sol;b><&sol;i>Illegal monitor operation&comma; such as waiting on an unlocked thread&period;<&sol;li>&NewLine;<li><i><b>IllegalStateException <&sol;b><&sol;i>Environment or application is in incorrect state&period;<&sol;li>&NewLine;<li><i><b>IllegalThreadStateException <&sol;b><&sol;i>Requested operation not compatible with current thread state&period;<&sol;li>&NewLine;<li><i><b>IndexOutOfBoundsException <&sol;b><&sol;i>Some type of index is out-of-bounds&period;<&sol;li>&NewLine;<li><i><b>NegativeArraySizeException <&sol;b><&sol;i>Array created with a negative size&period;<&sol;li>&NewLine;<li><i><b>NullPointerException <&sol;b><&sol;i>Invalid use of a null reference&period;<&sol;li>&NewLine;<li><i><b>NumberFormatException <&sol;b><&sol;i>Invalid conversion of a string to a numeric format&period;<&sol;li>&NewLine;<li><i><b>SecurityException <&sol;b><&sol;i>Attempt to violate security&period;<&sol;li>&NewLine;<li><i><b>StringIndexOutOfBounds <&sol;b><&sol;i>Attempt to index outside the bounds of a string&period;<&sol;li>&NewLine;<li><i><b>UnsupportedOperationException <&sol;b><&sol;i>An unsupported operation was encountered&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;Exception&period;png"&sol;><&sol;div>&NewLine;<p><b>Common scenarios of Exception Handling where exceptions may occur<&sol;b><br &sol;>&NewLine;There are given some scenarios where unchecked exceptions can occur&period; They are as follows&colon;<br &sol;>&NewLine;<b>1&rpar; Scenario where <i>ArithmeticException <&sol;i>occurs<&sol;b><br &sol;>&NewLine;If we divide any number by zero&comma; there occurs an <i><b>ArithmeticException<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">int a&equals;50&sol;0&semi;&sol;&sol;ArithmeticException &NewLine;<&sol;pre>&NewLine;<p><b><br &sol;>&NewLine;2&rpar; Scenario where <i>NullPointerException <&sol;i>occurs<&sol;b><br &sol;>&NewLine;If we have null value in any variable&comma; performing any operation by the variable occurs an <b><i>NullPointerException<&sol;i><&sol;b>&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">String s&equals;null&semi; &NewLine;System&period;out&period;println&lpar;s&period;length&lpar;&rpar;&rpar;&semi;&sol;&sol;NullPointerException &NewLine;<&sol;pre>&NewLine;<p><b>3&rpar; Scenario where <i>NumberFormatException <&sol;i>occurs<&sol;b><br &sol;>&NewLine;The wrong formatting of any value&comma; may occur <i><b>NumberFormatException<&sol;b><&sol;i>&period; Suppose I have a string variable that have characters&comma; converting this variable into digit will occur <b><i>NumberFormatException<&sol;i><&sol;b>&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">String s&equals;"abc"&semi; &NewLine;int i&equals;Integer&period;parseInt&lpar;s&rpar;&semi;&sol;&sol;NumberFormatException &NewLine;<&sol;pre>&NewLine;<p><b>4&rpar; Scenario where <i>ArrayIndexOutOfBoundsException <&sol;i>occurs<&sol;b><br &sol;>&NewLine;If you are inserting any value in the wrong index&comma; it would result <i><b>ArrayIndexOutOfBoundsException <&sol;b><&sol;i>as shown below&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">int a&lbrack;&rsqb;&equals;new int&lbrack;5&rsqb;&semi; &NewLine;a&lbrack;10&rsqb;&equals;50&semi; &sol;&sol;ArrayIndexOutOfBoundsException &NewLine;<&sol;pre>&NewLine;<p><b><br &sol;>&NewLine;<&sol;b> <b>Exceptions Methods&colon;<&sol;b><br &sol;>&NewLine;Following is the list of important methods available in the <b>Throwable <&sol;b>class&period;<&sol;p>&NewLine;<table class&equals;"src" style&equals;"-webkit-text-size-adjust&colon; auto&semi; -webkit-text-stroke-width&colon; 0px&semi; background-color&colon; &num;f7f7f7&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; color&colon; black&semi; font-family&colon; Helvetica&comma; Arial&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; normal&semi; margin&colon; 8px 0px&semi; orphans&colon; auto&semi; padding&colon; 0px&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; vertical-align&colon; top&semi; white-space&colon; normal&semi; widows&colon; auto&semi; width&colon; 560px&semi; word-spacing&colon; 0px&semi;">&NewLine;<tbody style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<th style&equals;"background-color&colon; &num;eeeeee&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi; text-align&colon; left&semi;">SN<&sol;th>&NewLine;<th style&equals;"background-color&colon; &num;eeeeee&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi; text-align&colon; left&semi;">Methods with Description<&sol;th>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">1<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public String getMessage&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Returns a detailed message about the exception that has occurred&period; This message is initialized in the Throwable constructor&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">2<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public Throwable getCause&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Returns the cause of the exception as represented by a Throwable object&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">3<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public String toString&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Returns the name of the class concatenated with the result of getMessage&lpar;&rpar;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">4<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public void printStackTrace&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Prints the result of toString&lpar;&rpar; along with the stack trace to System&period;err&comma; the error output stream&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">5<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public StackTraceElement &lbrack;&rsqb; getStackTrace&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Returns an array containing each element on the stack trace&period; The element at index 0 represents the top of the call stack&comma; and the last element in the array represents the method at the bottom of the call stack&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;">6<&sol;td>&NewLine;<td style&equals;"border-collapse&colon; collapse&semi; border&colon; 1px solid rgb&lpar;214&comma; 214&comma; 214&rpar;&semi; margin&colon; 0px&semi; padding&colon; 5px&semi;"><b style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">public Throwable fillInStackTrace&lpar;&rpar;<&sol;b><br &sol;>&NewLine;Fills the stack trace of this Throwable object with the current stack trace&comma; adding to any previous information in the stack trace&period;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;instance-initializer-block-in-java&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;try-catch-block-and-handling-exceptions&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;instance-initializer-block-in-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;try-catch-block-and-handling-exceptions&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '504'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago