×
☰ See All Chapters

What is Exception Handling in Java

what-is-exception-handling-0
 

Errors:

It is a runtime error which will cause the program to terminate. You cannot handle it. Error will terminate the program.

Actually, these are not exceptions at all, but Sun Microsystems says it is a type of Exception .These are problems that arise beyond the control of the user or the programmer.

E.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Errors are often ignored when designing and writing Java applications, Why?

Errors are typically ignored in your code because you can rarely do anything about an error, even if you wanted your program to fix the problem. For example, if a stack overflow occurs, an error will arise. However, because you are out of memory, your program will be unable to continue executing. Any code you have written that attempts to fix the problem won’t get a chance to execute anyway, so errors are often ignored when designing and writing Java applications.

Exceptions

It is a runtime error which can cause the program to terminate. We can handle the exception and if handled program does not  terminate and execution continues normally. Exception is an object thrown by a method.

Example:

ArithmeticException

int a=50/0;//ArithmeticException

NumberFormatException 

boolean  b=true;

int i=Integer.parseInt(s);

NullPointerException

Employee e1=null;

System.out.println(e.name);//NullPointerException

ArrayIndexOutOfBoundsException 

int a[]=new int[10];

System.out.println(a[20]);  //ArrayIndexOutOfBoundsException

Checked exceptions: If checked exception is present in program, then before doing anything to program we need to do something to exception. Since we have to do something to exception before doing anything to program it is a checked exception.

Unchecked exceptions: If unchecked exception is present in program, then we can do anything to program without doing anything to exception. Since we can proceed with program execution without doing anything to exception it is an unchecked exception.

Flow of Control of Exceptions

what-is-exception-handling-1
 
  • If method3() throws an exception, method3() is popped off the call stack, and the exception is thrown down to method2(). With an exception heading its way, method2 () has three choices: 

    1. Catch the exception so that it does not go any further down the call stack. 

    2. Catch the exception, and then throw it back down the call stack. 

    3. Not catch the exception, thereby causing method1() to be popped off the call stack, with the exception continuing down the call stack to main(). 

  • If method2() doesn’t catch the exception, method2() is popped off the call stack, and the exception is thrown down to method1(). With an exception heading its way, method2 () has three choices: 

    1. Catch the exception so that it does not go any further down the call stack. 

    2. Catch the exception, and then throw it back down the call stack. 

    3. Not catch the exception, thereby causing method1() to be popped off the call stack, with the exception continuing  down the call stack to main().  

  • If method1() doesn’t catch the exception, method1() is popped off the call stack, and the exception is thrown down to main method. Like this flow of control continues down the call stack, no matter how many methods appear on the call stack. Each method further down the call stack either catches the exception and stops this process, catches the exception and throws it again, or simply does nothing and lets the exception fall through to the next method. 

  • When an exception reaches the bottom of a call stack to the main method, main() ignores the exception  as well, main()  is popped off the call stack. The exception is then passed on to the JVM, JVM provides   a default exception handler and the JVM  prints out the stack trace and terminates. 

Note: We can print this stack trace yourself with any exception you catch by using the printStackTrace() method of the Throwable class.

Hierarchy of Exception classes

what-is-exception-handling-2
 

Exception Handling with Method Overriding

There are many rules if we talk about method overriding with exception handling. The Rules are as follows:

If the superclass method does not declare any exception: If the superclass method does not declare any exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

If the superclass method declares exception: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

How to handle Exception?

Five keywords used in Exception handling

  1. try 

  2. catch 

  3. finally 

  4. throw 

  5. throws 

These will be studied in detail in next chapters.

 

 


All Chapters
Author