SlideShare a Scribd company logo
1 of 18
{
PRESENTATION ON
Exception Handling
Group Name : Bug Free.
Group Members:
1.Sajibul Hasan 151-15-4986
2.Abdullah Al Noman 151-15-4853
3.Mehedi Hassan Khan 151-15-5154
4.Nahian Ahmed 151-15-5137
What is an exception?
*An exception is an error condition that
changes the normal flow of control in a
program
*When an Exception occurs the normal flow
of the program is disrupted and the
program/Application terminates
abnormally, which is not recommended,
therefore these exceptions are to be handled
Why Exception Occurs?
An exception can occur for many different
reasons, below given are some scenarios where
exception occurs.
>>A user has entered invalid data.
>>A file that needs to be opened cannot be found.
>>A network connection has been lost in the
middle of communications or the JVM has run
out of memory.
Exception Hierarchy
Exception Has two Main
classes :
1. Checked exceptions : known as
compile time exceptions.
Programmer should take care of
(handle) these exceptions
2. Unchecked exceptions : Known as
Runtime Exceptions.
These include programming bugs, such
as logic error also.
Checked exceptions
import java.io.File;
import java.io.FileReader;
public class FilenotFound_Demo {
public static void main(String args[]){
File file=new File("E://file.txt");
FileReader fr = new FileReader(file);
}
} Output: C:>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8:
error: unreported exception
FileNotFoundException; must be caught or
declared to be thrown
FileReader fr = new FileReader(file);
Example
Unchecked exceptions
Example
public class Unchecked_Demo {
public static void main(String
args[]){
int num[]={1,2,3,4};
System.out.println(num[5]);
}
} Output:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5 at
Exceptions.Unchecked_Demo.main(Unchecked_D
emo.java:8
Exception Handling Terms
1.Try – used to enclose a segment of code that may
produce a exception
2.Catch – placed directly after the try block to handle one
or more exception types
3.Throw – to generate an exception or to describe an
instance of an exception
4.Finally – optional statement used after a try-catch block
to run a segment of code regardless if a exception is
generated
Try – Catch Block
Try – used to enclose a segment of code that
may produce a exception
Catch – placed directly after the try block to
handle one or more exception types
try {
statements;
}
catch(Exception ex) {
perform operations before exits;
throw ex;
}
Multiple catch statements
try {
<code segment that may
throw an exception..>
} catch (IOException e) {
System.out.println(e.getMessage()
);
} catch (FileNotFoundException e){
System.out.println(“FileNotFound!”);
}
Nested try-catch block
try {
statement 1;
try {
statement 2;
statement 3;
}
catch(Exception e) { }
}
catch(Exception e) { }
By using Throw
THROW-generate an exception or to describe an instance
of an exception
Define a class:
public class EmptyStackException extends Exception {
}
Here is how you use the class:
public class Stack {
public Object Pop() throws EmptyStackException
{
if (Empty()) throw new EmptyStackException();
...
}
}
Note that you must use new to create an exception
object; you cannot just throw an exception.
Example
static class Exception2{
static int sum(int num1, int num2){
if (num1 == 0)
throw new ArithmeticException("First parameter is not
valid");
else
System.out.println("Both parameters are correct!!");
return num1+num2; }
public static void main(String args[]){
int res=sum(1,12);
System.out.println(res);
System.out.println("Continue Next statements");
}
}
The finally
try {
statements;
}
catch(TheExceptionex) {
handling ex;
}
finally {
finalStatements;
}
Example
public static void main(String[] arg){
try{
int i = 10/0;
} catch(Exception ex){
System.out.println("Inside 1st catch Block");
} finally {
System.out.println("Inside 1st finally block");
}
try{
int i = 10/10;
} catch(Exception ex){
System.out.println("Inside 2nd catch Block");
} finally {
System.out.println("Inside 2nd finally block");
}
} Inside 1st catch Block
Inside 1st finally block
Inside 2nd finally block
Thanks All

More Related Content

What's hot

What's hot (20)

Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java package
Java packageJava package
Java package
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 

Similar to Presentation on-exception-handling

UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingSakkaravarthiS1
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertionRakesh Madugula
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handlingHemant Chetwani
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javagopalrajput11
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxNagaraju Pamarthi
 

Similar to Presentation on-exception-handling (20)

Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
JAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdfJAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdf
 
Exception handling
Exception handlingException handling
Exception handling
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Presentation1
Presentation1Presentation1
Presentation1
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 

More from Nahian Ahmed

House Price Prediction An AI Approach.
House Price Prediction An AI Approach.House Price Prediction An AI Approach.
House Price Prediction An AI Approach.Nahian Ahmed
 
Vlsm and supernetting
Vlsm and supernettingVlsm and supernetting
Vlsm and supernettingNahian Ahmed
 
A presentation on android OS
A presentation on android OSA presentation on android OS
A presentation on android OSNahian Ahmed
 
Presentation on DNA Sequencing Process
Presentation on DNA Sequencing ProcessPresentation on DNA Sequencing Process
Presentation on DNA Sequencing ProcessNahian Ahmed
 
Presentation on 8086 Microprocessor
Presentation  on   8086 MicroprocessorPresentation  on   8086 Microprocessor
Presentation on 8086 MicroprocessorNahian Ahmed
 
Applocation of Numerical Methods
Applocation of Numerical MethodsApplocation of Numerical Methods
Applocation of Numerical MethodsNahian Ahmed
 
Presentation on Flip Flop
Presentation  on Flip FlopPresentation  on Flip Flop
Presentation on Flip FlopNahian Ahmed
 

More from Nahian Ahmed (11)

House Price Prediction An AI Approach.
House Price Prediction An AI Approach.House Price Prediction An AI Approach.
House Price Prediction An AI Approach.
 
IOT Smart House
IOT Smart HouseIOT Smart House
IOT Smart House
 
Vlsm and supernetting
Vlsm and supernettingVlsm and supernetting
Vlsm and supernetting
 
Data warehouse
Data warehouseData warehouse
Data warehouse
 
A presentation on android OS
A presentation on android OSA presentation on android OS
A presentation on android OS
 
Presentation on DNA Sequencing Process
Presentation on DNA Sequencing ProcessPresentation on DNA Sequencing Process
Presentation on DNA Sequencing Process
 
Delta Modulation
Delta ModulationDelta Modulation
Delta Modulation
 
Presentation on 8086 Microprocessor
Presentation  on   8086 MicroprocessorPresentation  on   8086 Microprocessor
Presentation on 8086 Microprocessor
 
Applocation of Numerical Methods
Applocation of Numerical MethodsApplocation of Numerical Methods
Applocation of Numerical Methods
 
Presentation on Flip Flop
Presentation  on Flip FlopPresentation  on Flip Flop
Presentation on Flip Flop
 
Game Architect
Game ArchitectGame Architect
Game Architect
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 

Presentation on-exception-handling

  • 1. { PRESENTATION ON Exception Handling Group Name : Bug Free. Group Members: 1.Sajibul Hasan 151-15-4986 2.Abdullah Al Noman 151-15-4853 3.Mehedi Hassan Khan 151-15-5154 4.Nahian Ahmed 151-15-5137
  • 2. What is an exception? *An exception is an error condition that changes the normal flow of control in a program *When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled
  • 3. Why Exception Occurs? An exception can occur for many different reasons, below given are some scenarios where exception occurs. >>A user has entered invalid data. >>A file that needs to be opened cannot be found. >>A network connection has been lost in the middle of communications or the JVM has run out of memory.
  • 5. Exception Has two Main classes : 1. Checked exceptions : known as compile time exceptions. Programmer should take care of (handle) these exceptions 2. Unchecked exceptions : Known as Runtime Exceptions. These include programming bugs, such as logic error also.
  • 7. import java.io.File; import java.io.FileReader; public class FilenotFound_Demo { public static void main(String args[]){ File file=new File("E://file.txt"); FileReader fr = new FileReader(file); } } Output: C:>javac FilenotFound_Demo.java FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr = new FileReader(file); Example
  • 9. Example public class Unchecked_Demo { public static void main(String args[]){ int num[]={1,2,3,4}; System.out.println(num[5]); } } Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Exceptions.Unchecked_Demo.main(Unchecked_D emo.java:8
  • 10. Exception Handling Terms 1.Try – used to enclose a segment of code that may produce a exception 2.Catch – placed directly after the try block to handle one or more exception types 3.Throw – to generate an exception or to describe an instance of an exception 4.Finally – optional statement used after a try-catch block to run a segment of code regardless if a exception is generated
  • 11. Try – Catch Block Try – used to enclose a segment of code that may produce a exception Catch – placed directly after the try block to handle one or more exception types try { statements; } catch(Exception ex) { perform operations before exits; throw ex; }
  • 12. Multiple catch statements try { <code segment that may throw an exception..> } catch (IOException e) { System.out.println(e.getMessage() ); } catch (FileNotFoundException e){ System.out.println(“FileNotFound!”); }
  • 13. Nested try-catch block try { statement 1; try { statement 2; statement 3; } catch(Exception e) { } } catch(Exception e) { }
  • 14. By using Throw THROW-generate an exception or to describe an instance of an exception Define a class: public class EmptyStackException extends Exception { } Here is how you use the class: public class Stack { public Object Pop() throws EmptyStackException { if (Empty()) throw new EmptyStackException(); ... } } Note that you must use new to create an exception object; you cannot just throw an exception.
  • 15. Example static class Exception2{ static int sum(int num1, int num2){ if (num1 == 0) throw new ArithmeticException("First parameter is not valid"); else System.out.println("Both parameters are correct!!"); return num1+num2; } public static void main(String args[]){ int res=sum(1,12); System.out.println(res); System.out.println("Continue Next statements"); } }
  • 16. The finally try { statements; } catch(TheExceptionex) { handling ex; } finally { finalStatements; }
  • 17. Example public static void main(String[] arg){ try{ int i = 10/0; } catch(Exception ex){ System.out.println("Inside 1st catch Block"); } finally { System.out.println("Inside 1st finally block"); } try{ int i = 10/10; } catch(Exception ex){ System.out.println("Inside 2nd catch Block"); } finally { System.out.println("Inside 2nd finally block"); } } Inside 1st catch Block Inside 1st finally block Inside 2nd finally block