In this core java training session, you will learn Exception Handling. Topics covered in this session are:
• Exception Handling
• Exception Class hierarchy
• Types of Exception
• Keywords for Exception Handling
For more information about this course visit on this link: https://www.mindsmapped.com/courses/software-development/learn-java-fundamentals-hands-on-training-on-core-java-concepts/
3. Page 2Classification: Restricted
Exceptions…
• One of the powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained.
• In Java, exception is an event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
• Exception Handling is a mechanism to handle runtime errors such as
ClassNotFound, IO, SQL, Remote etc.
6. Page 5Classification: Restricted
Types of Exception
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are
known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions
are checked at compile-time.
The programmer is compelled to handle (caught or declared) these in his code.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time rather they are checked at
runtime. These are exceptions that the programmer is not compelled to handle in his
code.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError
etc.
7. Page 6Classification: Restricted
Examples
• ArithmeticException
int a=50/0;//ArithmeticException
• NullPointerException
String s=null;
System.out.println(s.length());//NullPointerException
• NumberFormatException
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
• ArrayIndexOutOfBoundsException
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
9. Page 8Classification: Restricted
Problem without exception handling
public class Testtrycatch1{
public static void main(String args[]){
int data=50/0;//may throw exception
System.out.println("rest of the code...");
}
}
11. Page 10Classification: Restricted
Solution by exception handling
public class Testtrycatch2{
public static void main(String args[]){
try{
int data=50/0;
}catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}
12. Page 11Classification: Restricted
Example…
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
catch(Exception e){System.out.println("common task completed");}
System.out.println("rest of the code...");
}
}
Rule: At a time only one Exception occurs and at a time only one catch block is executed.
Rule: All catch blocks must be ordered from most specific to most general i.e. catch for
ArithmeticException must come before catch for Exception.
13. Page 12Classification: Restricted
Nested try catch blocks…
class Excep6{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e){System.out.println(e);}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
System.out.println("other statement);
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
}
14. Page 13Classification: Restricted
finally block
• Java finally block is a block that is used to execute important code
(releasing resources) such as closing connection, stream etc.
• Java finally block is always executed whether exception is handled or not.
• Java finally block follows try or catch block.
15. Page 14Classification: Restricted
Examples…
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
16. Page 15Classification: Restricted
Example: Exception occurs, not handled…but finally is executed.
class TestFinallyBlock1{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
17. Page 16Classification: Restricted
Example: Exception occurs and handled
public class TestFinallyBlock2{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
18. Page 17Classification: Restricted
More rules…
• Rule: For each try block there can be zero or more catch blocks, but only
one finally block.
• Note: The finally block will not be executed if program exits(either by
calling System.exit() or by causing a fatal error that causes the process to
abort)
19. Page 18Classification: Restricted
Topics to be covered in next session
• throw and throws keywords
• Exception propagation – the cases of Checked and Unchecked Exceptions
• Defining your own custom Exception