Exception Handling in Java

What is an Exception in Java ?

An Exception is an “exceptional condition” which interrupts the normal flow of the program. This “exceptional condition” can occur due to many reasons.

eg: Your are trying to divide a number by Zero, you are trying to access a file which does not exists, trying to access the members of a class through a reference pointing to null, a line of code which is trying connect to database server where as address of the db server is wrong etc.

 

For example :

 

In the above example even though Task 4 and Task 5 has got no dependency on the previous statements, they never get a turn to execute.

Divide by 0 is the simplest example for an abnormal condition.

 

Some more examples:

We are trying to read a file where we don’t have read permission. A piece of code which is communicating to  a system over network and that system goes down. We are trying to read the contents of an array beyond its size.

Lets compile and execute the above program and interpret it.

video

As we seen the above example the divided by zero is categorized as java.lang.ArithmeticException. This means that Exceptions are well categorized and there a class to represent different exceptions.

 

Redefining the Exceptions in Java

Everything in Java can be called as an Object. So an Exception is also an Object. An Exception is an instance of class Exception or its subclass thrown by JVM(*explained in detail in the next video).

Java developers has created different classes to represent different Exceptions and these classes has been well organised using inheritance which can be called as Exception hierarchy.

 

Exception Hierarchy Diagram ( ***VVIMP )

 

 

 

Exception Handling

Whenever we suspect a statement that it might result in an Exception then we surround it with try and catch to handle it. When we handle like this the program will not terminate but it will continue executing the statements which are present after the catch block. Below is the modified example of our previous example.

 

 

In the above example an exception do occur but program will not terminate, irrespective of whether there is an Exception or not Task 4 and Task 5 will execute. To understand it well please watch below video.

video

Redefining exception handling :

Whenever we suspect a statement that it might result in an Exception then we surround it with try and catch to handle it. We can say exception is handled only when an appropriate catch block is present. If appropriate catch block is not present then we can not say exception is handled even though the suspected  statement is inside try and catch ( inside try followed by catch).

For example the below program will terminate even though we have trouble causing statement inside try catch 🙂

 

 

Above program will result in below runtime exception:

 

 

try catch control flow :

video

try followed by multiple catch block :

video

Example:

As I explained in the above video,

One try block can be followed by more than one catch block.

Suppose if there is an exception in try block then the rest of the statements in try blocks are ignored and control will go to next immediate catch block. If it the matching catch block is present then it will execute and then the program will continue executing the statements followed.

If there is no exception in try block then catch block will never get chance to execute and program will continue executing the statements followed by catch block/blocks.

 

Note to all by beautiful students : My training is the combination of Videos, Notes and assignments. It’s not just video or notes or assignments. Some concepts may have been explained only in notes where as some may have been explained only in videos, same way some concepts I might have covered it in assignments only. I request you to go through all of it with sincerity.

@vinay

 

 Purpose of using more than one catch block :

The purpose of using more than one catch block is to handle different exception differently.

video

As I explained in the above video,
  • When you have more than one catch block the most specialized exception should be listed first.
  • If you specify generic exception in first catch block itself then the remaining catch blocks will never execute at all.

 

finally block :

video

Important points we learnt from above video:
  • A try block should be followed by either catch or finally.
  • Either try or catch or finally can not be alone.
  • try block can be followed by one or more catch block but there can be only one finally block.
  • If try is followed by both cath and finally then the sequence should be try followed by one or more catch block which should be followed by finally block.
  • Irrespective of whether there is an exception or not finally will always execute.
  • If try block is followed by finally and if there is an exception in try block then the control will go to finally and program will terminate !! as exception is not handled by providing the appropriate catch block.
  • If there is an exception in try block and if matching catch block is present then the catch block will execute and then finally will execute if it is present.
  • If there is no exception in try then finally will execute if finally is present.
  • Since finally will always execute it is typically used for clearing up the resources or to release the resource.

 

try catch & finally block scenarios ( VVIMP ) :

 

throw and throws :

video

Checked Exceptions and Unchecked Exceptions :

video

Creating custom exceptions :

video

What is an Error in Java ? What is the difference between Error & an Exception ?

 

 

Summary of Exceptions Chapter


Assertions in Java

Assertions in Java :