wellnwill Marathahalli Bangalore

Exception handing

  • Exception handing is the mechanism to handle the exception. The meaning of the exception is abnormal termination.
  • An exception is a problem occurred during execution time of the program and disturbs normal flow of execution.
  • In java, exception is also an object or instance. Exception is a class present in java.lang package.
  • Whenever JRE encounter any abnormal situation, it creates an object of Throwable class and the reference of Throwable class will be thrown.

In java, all exception object should be a type of Throwable class.

Example of Exception: invalid data entered by user.

Types of Exception

  • Checked Exception
  • Unchecked Exception

Checked Exception

  • The exception which are directly inherited from Exception class are known as checked exception. Checked exceptions are verified at compile time.
  • It is necessary to handle checked exception otherwise compiler will not compile the program. EX-FileNotFoundException, IOException, ClassNotFoundException etc.

Unchecked Exception

  • Exceptions which are inherited from RuntimeException class are known as an unchecked exception.
  • Unchecked exception are not checked at compile time. It is not mandatory to handle unchecked exception. Ex- ArithmeticException, NullPointerException, ClassCastException etc.

Note-The Exception whether it is checked or unchecked, it will be occurred at runtime.

Error

If system resources are not available then we get error and error are non-recoverable. Ex:- StackOverFlowError, OutOfMemoryError etc.

To handle the exceptions, java provides the following keywords.

  • try
  • catch
  • finally
  • throw
  • throws

Internal working of java try-catch block

try block

This block contains those set of statements whose execution may result an exception. Try block has a catch bock or finally block or both associated with it.

catch block

This block is used to hold an exception. It means the statements of catch block will be executed only if an exception will be generated in its try block.

Overloading of catch() block

We can specify multiple catch() block for a single try block but with different Throwable types of argumets.

try{
}
catch(ArithmeticException e){
}
catch(NullPointerException e){
}

Polymorphic behavior of catch() block

If a catch block handle multiple type of exceptions.

try{
}
catch(Exception e)
{
}

Note- We can use both specific and polymorphic catch block for a single try block but in this case the polymorphic catch must be the last handler for try block.

try{
}
catch(ArithmeticException e){
}
catch(NullPointerException e){
}
catch(Exception e){
}

try{
}
catch(Exception e){//invalid 
}

catch(ArithmeticException e){
}
catch(NullPointerException e){
}

Finally block:

  • This block can be written along with try or along with try catch.
  • Statement present within finally block with always be executed (whether exception will occur or not “AND” exception will be handled or not).
  • Within finally block we write cleanup statements such as closing a database connection, closing the used resources etc.
  • There should be only one finally block.
  • If we are using System.exit(0) then only finally block will not be executed.

Valid combinations

Invalid combinations

class Test224
{
public static void main(String[] args)
{
System.out.println(50/0);
}
}O/P-Exception in thread “main” java.lang.ArithmeticException: / by zero at Test224.main(Test224.java:5)
Note-here default handler of JVM prints this message to console by stack tracing process.
java.lang.ArithmeticException- Type of Exception
: / by zero- Exception description
stack tracing- at Test224.main(Test224.java:5)






class Test225
{
public static void main(String[] args)
{
System.out.println("main() starts");
try
{
System.out.println(50/0);
}
catch (ArithmeticException e)
{
System.out.println(e);
}
System.out.println("main() ends");
}}O/P-main() starts
          Java.lang.ArithmeticException: / by zero
           main() ends

When Exception is raised in try block then JVM will search for corresponding catch block to handle the exception if the corresponding catch block is present, it will be executed and rest of the code also.

The control will never transfer to same try block again.

class Test226
{
public static void main(String[] args)
{
System.out.println("main() starts");
try
{
System.out.println(50/0);
int[] a={1,2,3};
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
}
catch (ArithmeticException ae){
System.out.println(ae);
}
catch (ArrayIndexOutOfBoundsException aie){
System.out.println(aie);
}
System.out.println("main() ends");
}
}
// O/P-main() starts
         // Java.lang.ArithmeticException: / by zero
         //  main() ends
	
try with multiple catch
class Test227
{
public static void main(String[] args)
{
System.out.println("main() starts");
try
{
System.out.println(50/0);
}
catch (ArithmeticException ae){
System.out.println(ae);
}
try{
int[] a={1,2,3};
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
}
catch (ArrayIndexOutOfBoundsException aie){
System.out.println(aie);
}
System.out.println("main() ends");
}
} 
//O/P-main() starts
    //      Java.lang.ArithmeticException: / by zero
     //     1,2,3    java.langArrayIndexOutOfBoundsException
      //     main() ends
	



	  
Footer with Map

Top