+ All Categories
Home > Documents > Exception Handling in Java

Exception Handling in Java

Date post: 22-Jan-2017
Category:
Upload: ganesh-kumar-reddy
View: 209 times
Download: 0 times
Share this document with a friend
55
Exception - Handling
Transcript
Page 1: Exception Handling in Java

Exception - Handling

Page 2: Exception Handling in Java

WHAT IS EXCEPTION?The Unwanted event that occurred at runtime and disturbs the Normal flow of program is called “Exception”

The process of handling the Exceptions is called

“Exception Handling”

Page 3: Exception Handling in Java

DIFFERENCE BETWEEN ERROR AND EXCEPTIONError : Error is a problem for which we are unable to provide the solution programmaticallyEx: Linkage Error , Virtual Machine Error

Exception : Exception is a problem for which we are able to provide the Solution(Alternative)Ex: IOException , NullPointer Exception

We are not repairing the Exception

Page 4: Exception Handling in Java

TYPES OF TERMINATIONS IN THE APPLICATIONSSmooth termination

Terminating the application at the end of the program After completing the execution is called Smooth termination

AbNormal TerminationTerminating the application , while in the execution of

the application due to some events is called AbNormal Termination

Page 5: Exception Handling in Java

TYPES OF EXCEPTIONS IN JAVA1.Checked Exceptions

2.Unchecked Exceptions.

Even though exceptions arrives at runtime compiler is able to warn about some kind of exceptions when there is exception prone code at compile time.i.e., Exceptions that are checked at compile-time are Checked-ExceptionsExamples of Checked Exceptions:IOExceptionSQLException etc.

Exceptions that are not checked at compile-time are Unchecked-ExceptionsExamples of Unchecked Exceptions:NullPointerExceptionArrayIndexOutOfBoundIllegalArgumentException etc.

Page 6: Exception Handling in Java

EXCEPTION HIERARCHY

Page 7: Exception Handling in Java

Importance of try , catch , finally

Try{

}

catch() // Handling Code}

finally{// Clean up code

}

// Risky Code

Page 8: Exception Handling in Java

POINT TO BE REMEMBER…………..

With In the try block If Exception is Raised , then rest of the try block won’t be executed even though we handle that exception

Page 9: Exception Handling in Java

DISPLAYING THE EXCEPTION DETAILS1. PrintStackTrace()

2.toString()

3.getMessage()

Page 10: Exception Handling in Java

1. PrintStackTrace()

Name of the ExceptionDescription of the ExceptionLocation o the Exception

2.toString()Name of the ExceptionDescription of the Exception

3.getMessage()

Description of the Exception

Page 11: Exception Handling in Java

CONTROL-FLOW IN TRY & CATCH

Page 12: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}

Catch(Exception e){statement 4;

}

statement 5;

Page 13: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}

Catch(Exception e){statement 4;

}

statement 5;

Case 1: If there is no Exception

Statements executed

statement 1;statement 2;statement 3;statement 5;

Normal

Page 14: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}statement 5;

Case 2: Exception is raised at statement2 & is Handled(catch block is matched)

Statements executed

statement 1;statement 4;statement 5;

Normal

Page 15: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}statement 5;

Case 3 : Exception is raised at statement2 & corresponding catch block is not provided

Statements executed

only statement 1;

Ab-Normal

Page 16: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}

Catch(Exception e){statement 4;

}

statement 5;

Case 4 Exception is raised at statement4 (or) statement5

Statements executed

Up to Preceding statement before Exception

Ab-Normal

Page 17: Exception Handling in Java

POINT TO BE REMEMBER…………..

If any Exception is raised outside the try block , it is always leads to the AbNormal termination of the program

Page 18: Exception Handling in Java

finally block

finally block is always associated with try & catch

It can be used to execute a set of instructions irrespective of that ,whether the exception occurred or not and that exception is handled or not

Always finally block is executed

Page 19: Exception Handling in Java

class Test1{

public static void main(String args[]]){

Try{System.out.println(“try”);

}

catch(Exception e){System.out.println(“catch”);

}finally{

System.out.println(“finally”);}

}

}

outputtryfinally

Page 20: Exception Handling in Java

Control-Flow in try , catch & finally

Page 21: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}finally{

statement 5;}

statement 6;

Page 22: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}

statement 6;

Case 1: If there is no Exception

Statements executed

statement 1;statement 2;statement 3;statement 5;

Normalfinally{ statement 5;

}

statement 6;

Page 23: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}

statement 6;

Case 2 Exception is raised at statement2 & corresponding catch block is matched

Statements executed

statement 1;statement 4;statement 5;

Normalfinally{ statement 5;

}

statement 6;

Page 24: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){

statement 4;}

statement 6;

Case 3 Exception is raised at statement2 & corresponding catch block is not matched

Statements executed

statement 1;statement 5;

Ab-Normal

finally{ statement 5;

}

Page 25: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){statement 4;

}

statement 6;

Case 4 Exception is raised at statement4 i.e. , the Exception already occurred at statement1 or statement2 or statement 3

Statements executed

Ab Normalfinally{

statement 5;}

Up to Preceding statement before Exception Statement 5

Page 26: Exception Handling in Java

Try{statement 1;statement 2;statement 3;

}Catch(Exception e){statement 4;

}

statement 6;

Case 5 Exception is raised at statement5 (or) statement6

Statements executed

Ab Normalfinally{

statement 5;}

Up to Preceding statement before Exception Statement 5

Page 27: Exception Handling in Java

class Test1{

public static void main(String args[]]){

Try{System.out.println(10/0);System.out.println(“try”);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println(“catch”);}finally{

System.out.println(“finally”);}

}}

outputFinallyException in Thread ……

System.out.println(“out side finally”);

Page 28: Exception Handling in Java

‘finally’ dominates the return statement also

Page 29: Exception Handling in Java

class Return{ public static void main(String as[]) { try{ System.out.println("try"); return; } catch(Exception e){ System.out.println("catch"); } finally{ System.out.println("fanally"); } System.out.println("outside finally");

}}

Page 30: Exception Handling in Java

finally won’t executed in only one situation

If we shutdown the jvm , finally not executed

Page 31: Exception Handling in Java

class Shutdown{

public static void main(String args[]]){

Try{System.out.println(“try”); System.exit(0);

}catch(Exception e){

System.out.println(“catch”);}finally{

System.out.println(“finally”);}

}}

output

try

System.out.println(“out side finally”);

Page 32: Exception Handling in Java

Is it possible to write try , catch finally inside any of these blocks??

Yes, Absolutely possible

Page 33: Exception Handling in Java

Inside the try block

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

finally{}

}catch(Exception e){} finally{}

public static void main(String args[]){

}

Page 34: Exception Handling in Java

Inside the catch block

try{}catch(Exception e){

try{}catch(Exception e){}

finally{}

} finally{}

public static void main(String args[]){

}

Page 35: Exception Handling in Java

Inside the finally block

try{}catch(Exception e){} finally{

try{}catch(Exception e){}

finally{}

}

public static void main(String args[]){

}

Page 36: Exception Handling in Java

try with multiple catchesThe way handling the Exception is varied from Exception to Exception

Hence for every Exception , it is highly recommended to write a separate catch block

Page 37: Exception Handling in Java

Syntax : try{}catch(Exception e1){} catch(Exception e2){}catch(Exception e3){}

Page 38: Exception Handling in Java

POINT TO BE REMEMBER ……

In this case the order of catch block is very important

It should be from child to parent

If we will place them in the order from parent to childC.T.E : “Exception ExceptionName has already been caught

Page 39: Exception Handling in Java

Animal

Pet Animal Wild Animal

DOG

CAT TIGER

LION

Page 40: Exception Handling in Java

Send Try{

}catch(Dog ){

Sopln(“It is a Dog”);}

catch(Cat ){Sopln(“It is a cat”);

}

catch(Tiger){Sopln(“It is a tiger”);

}

catch(Wild Animal){Sopln(“It is a Wild

Animal”);}

catch(Animal ){Sopln(“It is a Animal”);

}

Dog It is a Dog

Output

Page 41: Exception Handling in Java

Send Try{

}

catch(Dog )Sopln(“It is a Dog”);

}

catch(Cat )Sopln(“It is a cat”);

}

catch(Wild Animal)Sopln(“It is a Wild

Animal”);}

catch(Lion)Sopln(“It is a Lion”);

}

catch(Animal ){Sopln(“It is a Animal”);

}

Dog It is a Animal

Output

Page 42: Exception Handling in Java

Class Test3{public static void main(String args[]){

try{int[] a=newint[3];a[0]=1;a[100]=2;

}catch(Exception e){

System.out.println(“Exception”);}catch(ArrayIndexOutOfBoundsException e){

System.out.println(“AIOOBE”);}

}}

C.T.E The Exception has already been caught

Page 43: Exception Handling in Java

DEFAULT EXCEPTION HANDLING IN JAVAIf in any method an Exception is raised , then that method is responsible for creation of the Exception object by including the Exception details

After creating the Exception object , the method handovers that Object to the jvm

The jvm checks for the Exception handling code in that method

If it is available , then jvm continues the rest of program execution After executing handling code

If it is not available , jvm terminates the execution abnormally & removes the corresponding entry from the stack

Page 44: Exception Handling in Java

Just before terminating the program abnormally , jvm handovers the responsibility of Exception handling to Default Exception Handler

Default Exception Handler just print the Exception information on the console in the following formatName of the Exception : DescriptionStack Trace(Location o the Exception)

Page 45: Exception Handling in Java

class Test1{

public static void main(String args[]]){

System.out.println(10/0);

}

}

Page 46: Exception Handling in Java

throws The main purpose of the throws keyword is to bypass the Exception handling from current method to caller if we are not interested to handle it

Page 47: Exception Handling in Java

class Throws2{

public static void m1(){ m2();} public static void m2(){ System.out.println(10/0);} public static void main(String args[]){ Throws2.m1();}

}

Page 48: Exception Handling in Java

class Throws2{

public static void m1(){ m2();} public static void m2(){ System.out.println(10/0);} public static void main(String args[]){ Throws2.m1();}

}

Page 49: Exception Handling in Java

throw The main purpose of the throw key word is to raise the Exception intentionally based on our application requirement

Point to be remember ……

After using the “throw” statement , If we will use any other statements ,then we will get compile-time error

Page 50: Exception Handling in Java

Example :

Public static void main(String args[]){throw new ArithmeticException(“My Exception”)System.out.println(“Hello”);

}

class A{

}

C.T.E : Unreachable statementSystem.out.println(“Hello”);

Page 51: Exception Handling in Java

STEPS TO CREATE USER DEFINED EXCEPTIONS

There are 3 steps to create to create user defined Exceptions

Page 52: Exception Handling in Java

STEP-1 : Create one user defined Exception class as a child class to

java.lang.Throwable

java.lang.Exception

C lass InvalidMarks extends Exception{

}

Page 53: Exception Handling in Java

STEP-2 : Prepare One String Parametrized constructor (To hold

the Exception Details) With the “super” statement

Class InvalidMarks extends Exception {

InvalidMarks(String s){super(s);

}}

Page 54: Exception Handling in Java

STEP-3 : Raise the Exception intentionally based on our

application requirement

If(smarks<0 || smarks>100){

throw new InvalidMarks(“Marks are Invalid”);

}

Page 55: Exception Handling in Java

Presentation ByGanesh Kumar Reddy


Recommended