SlideShare a Scribd company logo
Exception Handling in Java
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”
Error : Error is a problem for which we are unable to provide
the solution programmatically
Ex: 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
Smooth termination
Terminating the application at the end of the program
After completing the execution is called Smooth termination
AbNormal Termination
Terminating the application , while in the execution of
the application due to some events is called AbNormal
Termination
1.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-Exceptions
Examples of Checked Exceptions:
IOException
SQLException etc.
Exceptions that are not checked at compile-time are
Unchecked-Exceptions
Examples of Unchecked Exceptions:
NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException etc.
Exception Handling in Java
Importance of try , catch , finally
Try{
}
catch()
// Handling Code
}
finally{
// Clean up code
}
// Risky Code
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
1. PrintStackTrace()
2.toString()
3.getMessage()
1. PrintStackTrace()
Name of the Exception
Description of the Exception
Location o the Exception
2.toString()
Name of the Exception
Description of the Exception
3.getMessage()
Description of the Exception
Exception Handling in Java
Try{
statement 1;
statement 2;
statement 3;
}
Catch(Exception e){
statement 4;
}
statement 5;
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
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
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
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
If any Exception is raised outside the try block , it is always
leads to the AbNormal termination of the program
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
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”);
}
}
}
try
finally
Control-Flow in try , catch
& finally
Try{
statement 1;
statement 2;
statement 3;
}
Catch(Exception e){
statement 4;
}
finally{
statement 5;
}
statement 6;
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;
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;
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;
}
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 Normal
finally{
statement 5;
}
Up to Preceding statement
before Exception
Statement 5
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 Normal
finally{
statement 5;
}
Up to Preceding statement
before Exception
Statement 5
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”);
}
}
}
output
Finally
Exception in
Thread ……
System.out.println(“out side
finally”);
‘finally’ dominates the
return statement also
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");
}
}
finally won’t executed in
only one situation
If we shutdown the jvm , finally not executed
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”);
Is it possible to write try , catch
finally inside any of these blocks??
Yes, Absolutely possible
Inside the try block
try{
try{
}
catch(Exception e){
}
finally{
}
}
catch(Exception e){
}
finally{
}
public static void main(String args[]){
}
Inside the catch block
try{
}
catch(Exception e){
try{
}
catch(Exception e){
}
finally{
}
}
finally{
}
public static void main(String args[]){
}
Inside the finally block
try{
}
catch(Exception e){
}
finally{
try{
}
catch(Exception e){
}
finally{
}
}
public static void main(String args[]){
}
try with multiple catches
The way handling the Exception is varied from
Exception to Exception
Hence for every Exception , it is highly
recommended to write a separate catch
block
Syntax :
try{
}
catch(Exception e1){
}
catch(Exception e2){
}
catch(Exception e3){
}
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 child
C.T.E : “Exception ExceptionName has already been
caught
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
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
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
If 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
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 format
Name of the Exception : Description
Stack Trace(Location o the Exception)
class Test1{
public static void main(String args[]]){
System.out.println(10/0);
}
}
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
class Throws2
{
public static void m1()
{
m2();
}
public static void m2()
{
System.out.println(10/0);
}
public static void main(String args[])
{
Throws2.m1();
}
}
class Throws2
{
public static void m1(){
m2();
}
public static void m2(){
System.out.println(10/0);
}
public static void main(String args[]){
Throws2.m1();
}
}
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
Example :
Public static void main(String args[]){
throw new ArithmeticException(“My Exception”)
System.out.println(“Hello”);
}
class A{
}
C.T.E : Unreachable statement
System.out.println(“Hello”);
There are 3 steps to create to create
user defined Exceptions
Create one user defined Exception class as a child
class to
java.lang.Throwable
java.lang.Exception
C lass InvalidMarks extends Exception{
}
Prepare One String Parametrized constructor (To
hold the Exception Details) With the “super” statement
Class InvalidMarks extends Exception {
InvalidMarks(String s){
super(s);
}
}
Raise the Exception intentionally based on
our application requirement
If(smarks<0 || smarks>100){
throw new InvalidMarks(“Marks are Invalid”);
}
Presentation By
Ganesh Kumar Reddy
Ad

More Related Content

What's hot (20)

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
Deepak Tathe
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
Richard Paul
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
QaziUmarF786
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Creating your own exception
Creating your own exceptionCreating your own exception
Creating your own exception
TharuniDiddekunta
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
sharqiyem
 
JQuery selectors
JQuery selectors JQuery selectors
JQuery selectors
chandrashekher786
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 
Exception Handling
Exception Handling Exception Handling
Exception Handling
Swapnali Pawar
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
Deepak Tathe
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
Richard Paul
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
QaziUmarF786
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
sharqiyem
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
Kuntal Bhowmick
 

Viewers also liked (20)

Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
Mavoori Soshmitha
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
exception handling
exception handlingexception handling
exception handling
Manav Dharman
 
Java packages
Java packagesJava packages
Java packages
Shreyans Pathak
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
Arindam Ghosh
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second Version
Lemi Orhan Ergin
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
Deepak Sharma
 
java packages
java packagesjava packages
java packages
aptechsravan
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 
Java packages
Java packagesJava packages
Java packages
BHUVIJAYAVELU
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
parag
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
raksharao
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
Mavoori Soshmitha
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
Arindam Ghosh
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Java Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second VersionJava Exception Handling Best Practices - Improved Second Version
Java Exception Handling Best Practices - Improved Second Version
Lemi Orhan Ergin
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
Deepak Sharma
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
kamal kotecha
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
parag
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Ad

Similar to Exception Handling in Java (20)

8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
happycocoman
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Ferdin Joe John Joseph PhD
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
SukhpreetSingh519414
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
EduclentMegasoftel
 
Exception handling
Exception handlingException handling
Exception handling
priyaankasrivastavaa
 
Java unit3
Java unit3Java unit3
Java unit3
Abhishek Khune
 
Exception Handling,finally,catch,throw,throws,try.pptx
Exception Handling,finally,catch,throw,throws,try.pptxException Handling,finally,catch,throw,throws,try.pptx
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
Fraz Bakhsh
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
Narayana Swamy
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
Md. Tanvir Hossain
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
happycocoman
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception Handling,finally,catch,throw,throws,try.pptx
Exception Handling,finally,catch,throw,throws,try.pptxException Handling,finally,catch,throw,throws,try.pptx
Exception Handling,finally,catch,throw,throws,try.pptx
ArunPatrick2
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Ad

Exception Handling in Java

  • 2. 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”
  • 3. Error : Error is a problem for which we are unable to provide the solution programmatically Ex: 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
  • 4. Smooth termination Terminating the application at the end of the program After completing the execution is called Smooth termination AbNormal Termination Terminating the application , while in the execution of the application due to some events is called AbNormal Termination
  • 5. 1.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-Exceptions Examples of Checked Exceptions: IOException SQLException etc. Exceptions that are not checked at compile-time are Unchecked-Exceptions Examples of Unchecked Exceptions: NullPointerException ArrayIndexOutOfBound IllegalArgumentException etc.
  • 7. Importance of try , catch , finally Try{ } catch() // Handling Code } finally{ // Clean up code } // Risky Code
  • 8. 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
  • 10. 1. PrintStackTrace() Name of the Exception Description of the Exception Location o the Exception 2.toString() Name of the Exception Description of the Exception 3.getMessage() Description of the Exception
  • 12. Try{ statement 1; statement 2; statement 3; } Catch(Exception e){ statement 4; } statement 5;
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. If any Exception is raised outside the try block , it is always leads to the AbNormal termination of the program
  • 18. 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
  • 19. 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”); } } } try finally
  • 20. Control-Flow in try , catch & finally
  • 21. Try{ statement 1; statement 2; statement 3; } Catch(Exception e){ statement 4; } finally{ statement 5; } statement 6;
  • 22. 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;
  • 23. 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;
  • 24. 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; }
  • 25. 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 Normal finally{ statement 5; } Up to Preceding statement before Exception Statement 5
  • 26. 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 Normal finally{ statement 5; } Up to Preceding statement before Exception Statement 5
  • 27. 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”); } } } output Finally Exception in Thread …… System.out.println(“out side finally”);
  • 29. 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"); } }
  • 30. finally won’t executed in only one situation If we shutdown the jvm , finally not executed
  • 31. 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”);
  • 32. Is it possible to write try , catch finally inside any of these blocks?? Yes, Absolutely possible
  • 33. Inside the try block try{ try{ } catch(Exception e){ } finally{ } } catch(Exception e){ } finally{ } public static void main(String args[]){ }
  • 34. Inside the catch block try{ } catch(Exception e){ try{ } catch(Exception e){ } finally{ } } finally{ } public static void main(String args[]){ }
  • 35. Inside the finally block try{ } catch(Exception e){ } finally{ try{ } catch(Exception e){ } finally{ } } public static void main(String args[]){ }
  • 36. try with multiple catches The way handling the Exception is varied from Exception to Exception Hence for every Exception , it is highly recommended to write a separate catch block
  • 38. 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 child C.T.E : “Exception ExceptionName has already been caught
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. If 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
  • 44. 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 format Name of the Exception : Description Stack Trace(Location o the Exception)
  • 45. class Test1{ public static void main(String args[]]){ System.out.println(10/0); } }
  • 46. 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
  • 47. class Throws2 { public static void m1() { m2(); } public static void m2() { System.out.println(10/0); } public static void main(String args[]) { Throws2.m1(); } }
  • 48. class Throws2 { public static void m1(){ m2(); } public static void m2(){ System.out.println(10/0); } public static void main(String args[]){ Throws2.m1(); } }
  • 49. 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
  • 50. Example : Public static void main(String args[]){ throw new ArithmeticException(“My Exception”) System.out.println(“Hello”); } class A{ } C.T.E : Unreachable statement System.out.println(“Hello”);
  • 51. There are 3 steps to create to create user defined Exceptions
  • 52. Create one user defined Exception class as a child class to java.lang.Throwable java.lang.Exception C lass InvalidMarks extends Exception{ }
  • 53. Prepare One String Parametrized constructor (To hold the Exception Details) With the “super” statement Class InvalidMarks extends Exception { InvalidMarks(String s){ super(s); } }
  • 54. Raise the Exception intentionally based on our application requirement If(smarks<0 || smarks>100){ throw new InvalidMarks(“Marks are Invalid”); }
  翻译: