1.Exception Handling in Java | Java Exceptions - Javatpoint
1.Exception Handling in Java | Java Exceptions - Javatpoint
ADVERTISEMENT
The Exception Handling in Java is one of the powerful mechanism to handle the
runtime errors so that the normal flow of the application can be maintained.
In this tutorial, we will learn about Java exceptions, it's types, and the difference
between checked and unchecked exceptions.
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 1 of 16
:
Dictionary Meaning: Exception is an abnormal condition.
ADVERTISEMENT
In Java, an exception is an event that disrupts the normal flow of the program. It is
an object which is thrown at runtime.
ADVERTISEMENT
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 2 of 16
:
Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application; that
is why we need to handle exceptions. Let's consider a scenario:
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;
ADVERTISEMENT
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 3 of 16
:
Suppose there are 10 statements in a Java program and an exception occurs at
statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will
not be executed. However, when we perform exception handling, the rest of the
statements will be executed. That is why we use exception handling in Java.
Do You Know?
What are the 4 rules for using exception handling with method
overriding?
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 4 of 16
:
Hierarchy of Java Exception classes
The java.lang.Throwable class is the root class of Java Exception hierarchy inherited
by two subclasses: Exception and Error. The hierarchy of Java Exception classes is
given below:
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 5 of 16
:
There are mainly two types of exceptions: checked and unchecked. An error is
considered as the unchecked exception. However, according to Oracle, there are
three types of exceptions namely:
1. Checked Exception
2. Unchecked Exception
3. Error
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 6 of 16
:
Difference between Checked and Unchecked
Exceptions
1) Checked Exception
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 7 of 16
:
The classes that directly inherit the Throwable class except RuntimeException and
Error are known as checked exceptions. For example, IOException, SQLException,
etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked exceptions.
For example, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at
compile-time, but they are checked at runtime.
3) Error
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 8 of 16
:
Error is irrecoverable. Some example of errors are OutOfMemoryError,
VirtualMachineError, AssertionError etc.
Keyword Description
try The "try" keyword is used to specify a block where we should place
an exception code. It means we can't use try block alone. The try
block must be followed by either catch or finally.
finally The "finally" block is used to execute the necessary code of the
program. It is executed whether an exception is handled or not.
JavaExceptionExample.java
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 9 of 16
:
1. public class JavaExceptionExample{
2. public static void main(String args[]){
3. try{
4. //code that may raise exception
5. int data=100/0;
6. }catch(ArithmeticException e){System.out.println(e);}
7. //rest code of the program
8. System.out.println("rest of the code...");
9. }
10. }
Test it Now
Output:
1. int a=50/0;//ArithmeticException
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 10 of 16
:
2) A scenario where NullPointerException occurs
If we have a null value in any variable, performing any operation on the variable
throws a NullPointerException.
1. String s=null;
2. System.out.println(s.length());//NullPointerException
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 11 of 16
:
3. Java Nested Try
← Prev Next →
Feedback
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 12 of 16
:
Splunk tutorial SPSS tutorial Swagger tutorial
Regex tutorial
RxJS tutorial
Keras tutorial
Preparation
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 13 of 16
:
Interview Questions Company Questions
Trending Technologies
B.Tech / MCA
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 14 of 16
:
DBMS tutorial DAA tutorial
Operating System
Ethical Hacking
html tutorial
Automata Tutorial
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 15 of 16
:
Data Mining Data Warehouse
https://www.javatpoint.com/exception-handling-in-java 18/06/24, 8 24 PM
Page 16 of 16
: