Exception Handling in Java

 Exception handling in Java

=====================================

  • Introduction 
  • Runtime stack mechanism 
  • Default exception handling in java
  • Exception hierarchy
  • Customized exception handling by using try-catch
  • Control flow in try catch
  • Methods to print exception information
  • try with multiple catch block
  • finally block
  • Difference between final, finally and finalize
  • Control flow in try-catch-finally (next nested)
  • Various possible combination of try-catch-finally
  • throw keyword
  • throws keyword
  • Exception handling keyword summary
  • Various possible compile time errors in exception handling
  • Customized or user define exception
  • Top 10 exceptions
  • 1.7 version enhancements
    • try with resource 
    • Multi catch block


Introduction

  • An unexpected unwanted event that disturb normal flow of program, is called exception.
  • It is highly recommended to handle exception, and the main objective of exception handling is graceful termination of the program.
  • Exception handling does not mean repairing an exception. We have to provide alternative way to continue rest of the program normally.

Ex.

Our programming requirement is read data from remote file locating at London. At runtime if London file is not available, our program should be terminated abnormally. We have to provide some local file to continue rest of the program normally.
 

 

Runtime stack mechanism

  • For every thread JVM will create a runtime stack.
  • Each and every method call performed by that thread will be stored in corresponding stack.
  • Each entry in stack is called stack frame or activation record.
  • After completing every method call the corresponding entry from the stack will be removed.
  • After completing all method calls the stack will become empty and that empty stack be destroyed by JVM just before terminating the thread.

Ex: 

Default Exception Handling in Java

  • Inside a method if any exception occurs, the method in which is raised is responsible to create exception object by including the following information.
      • Name of Exception
      • Description of exception
      • Location  at which exception occurs( stack trace )
  •  After creating exception object method hand over that object to the JVM.
  • JVM will check whether the method contains any exception handling code or not. If the  method does not contain exception handling code, then JVM terminates that method abnormally and remove corresponding entry from the stack. 
  • Then JVM identify caller method and check whether caller method contain any handling code or not.
  • If the caller method does not contain handling code then JVM terminates that caller method abnormally and remove the corresponding entry from the stack.
  • This process will be continued until main method and if the main method also does not contain handling code, then JVM terminates main method abnormally and remove the corresponding entry from the stack.
  • Then JVM hand over responsibility of exception handling to Default exception handler, which is the part of JVM. 
  • Default exception handler prints exception information in the following format and terminate program abnormally.

Ex:


Note:

  • In a program at least one method terminates abnormally then the program termination is abnormal termination.
  • If all methods terminated normally then only program termination is normal termination. 
 
 
RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post