SlideShare a Scribd company logo
Exception Handling in Java
Explained By:
Sarbjit Kaur.
Lecturer, Department of Computer Application,
PGG.C.G., Sector: 42, Chandigarh
Objectives
• Introduction
• What exceptions are for
• Catching & Throwing exceptions
• Exception Specifications
• Standard Java Exceptions
• Exceptions and Polymorphism
• The finally clause
• Resource Management
• Uncaught Exceptions
Introduction
• Due to design errors or coding errors, our
programs may fail in unexpected ways during
execution. An exception is a condition that is
caused by run time error in the program. The
purpose of the exception handling mechanism
is to provide a means to detect and report an
“ecxceptional circumstances” .
Error
• An error may produce an incorrect output or
may terminate the execution of the program
abruptly or even may cause the system to
crash. So it is our responsibility to detect and
manage the error properly.
Types of error
• Runtime Errors: occur while the program is
running if the environment detects an
operation that is impossible to carry out.
• Logic Errors: occur when a program doesn't
perform the way it was intended
• Syntax Errors: Arise because the rules of the
language have not been followed. They are
detected by the compiler.
Example of Run Time error
Class Error
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b+c);
System.out.println("x=" +x);
int y=a/(b-c); // Errorr division by zero
System.out.println("y=" +y);
}
}
Errors and Error Handling
• Some typical causes of errors:
– Memory errors (i.e. memory incorrectly allocated,
memory leaks, “null pointer”)
– File system errors (i.e. disk is full, disk has been
removed)
– Network errors (i.e. network is down, URL does
not exist)
– Calculation errors (i.e. divide by 0)
Errors and Error Handling
• More typical causes of errors:
– Array errors (i.e. accessing element –1)
– Conversion errors (i.e. convert ‘q’ to a number)
– Can you think of some others?
Errors and Error Handling
• Exceptions – a better error handling
– Exceptions are a mechanism that provides the
best of both worlds.
– Exceptions act similar to method return flags in
that any method may raise and exception should it
encounter an error.
– Exceptions act like global error methods in that
the exception mechanism is built into Java;
exceptions are handled at many levels in a
program, locally and/or globally.
Exceptions
• How do you handle exceptions?
– To handle the exception, you write a “try-catch”
block. To pass the exception “up the chain”, you
declare a throws clause in your method or class
declaration.
– If the method contains code that may cause a
checked exception, you MUST handle the
exception OR pass the exception to the parent
class (remember, every class has Object as the
ultimate parent)
Coding Exceptions
• Coding Exceptions
• Try-Catch Mechanism
– Wherever your code may trigger an exception, the
normal code logic is placed inside a block of code
starting with the “try” keyword:
– After the try block, the code to handle the
exception should it arise is placed in a block of
code starting with the “catch” keyword.
Standard Java Exceptions
Throwable
Exception Error
Runtime
Exception
IO Exception
Catching Exceptions
• Wrap code to be checked in a try-block
– checking occurs all the way down the execution
stack
• try-blocks can be nested
– control resumes at most enclosed matching
handler
Coding Exceptions
• Example
– try {
… normal program code
}
catch(Exception e) {
… exception handling code
}
Coding Exceptions
• Types of Exceptions
– Examples:
• public void myMethod throws Exception {
• public void myMethod throws IOException {
• try { … }
catch (Exception e) { … }
• try { … }
catch (IOException ioe) { … }
Code Examples
• 1. Demonstration of an unchecked exception
(NullPointerException)
• 2. Demonstration of checked exceptions:
– Passing a DivideByZeroException
– Handling a DivideByZeroException
Example
class error2
{
public static void main(String arg[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println(“Division by Zero”);
}
Y=a/(b-c);
System.out.println(“y=“+y);
}
}
In the previous program we cannot see the
value of x just because of the error in the
value of y, that is division by zero but when we
use the try and catch blocks in exception
handling then we can see the value of y which
is correct and our program will display an
error message shown in the try block.
conclusion
– Exceptions are a powerful error handling
mechanism.
– Exceptions in Java are built into the language.
– Exceptions can be handled by the programmer
(try-catch), or handled by the Java environment
(throws).
– Exception handling can only hide the errors.
– It cannot correct the errors.
Ad

More Related Content

Similar to exception-handling-in-java.ppt (20)

using Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptxusing Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
JAVA Presenttation topics Programs.pptx
JAVA  Presenttation topics Programs.pptxJAVA  Presenttation topics Programs.pptx
JAVA Presenttation topics Programs.pptx
RitikSharma685066
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
SanthiNivas
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
MaqdamYasir
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
Exception handling
Exception handlingException handling
Exception handling
Abhishek Pachisia
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptions
Asif Tasleem
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Exception handling
Exception handlingException handling
Exception handling
Karthik Sekar
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Um presentation (1)
Um presentation (1)Um presentation (1)
Um presentation (1)
Ethel Joy Sumaljag
 
Java exeception handling
Java exeception handlingJava exeception handling
Java exeception handling
baabtra.com - No. 1 supplier of quality freshers
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Exception hierarchy
Exception hierarchyException hierarchy
Exception hierarchy
Ashfaaq Mahroof
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
using Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptxusing Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
JAVA Presenttation topics Programs.pptx
JAVA  Presenttation topics Programs.pptxJAVA  Presenttation topics Programs.pptx
JAVA Presenttation topics Programs.pptx
RitikSharma685066
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
SanthiNivas
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
MaqdamYasir
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptions
Asif Tasleem
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 

Recently uploaded (20)

Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdfDahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
PawachMetharattanara
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
UNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdfUNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdf
sikarwaramit089
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
Guru Nanak Technical Institutions
 
Urban Transport Infrastructure September 2023
Urban Transport Infrastructure September 2023Urban Transport Infrastructure September 2023
Urban Transport Infrastructure September 2023
Rajesh Prasad
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)
vijimech408
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFTDeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
Kyohei Ito
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdfDahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
Dahua Smart Cityyyyyyyyyyyyyyyyyy2025.pdf
PawachMetharattanara
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
UNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdfUNIT 3 Software Engineering (BCS601) EIOV.pdf
UNIT 3 Software Engineering (BCS601) EIOV.pdf
sikarwaramit089
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Urban Transport Infrastructure September 2023
Urban Transport Infrastructure September 2023Urban Transport Infrastructure September 2023
Urban Transport Infrastructure September 2023
Rajesh Prasad
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)
vijimech408
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFTDeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
Kyohei Ito
 
Ad

exception-handling-in-java.ppt

  • 1. Exception Handling in Java Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh
  • 2. Objectives • Introduction • What exceptions are for • Catching & Throwing exceptions • Exception Specifications • Standard Java Exceptions • Exceptions and Polymorphism • The finally clause • Resource Management • Uncaught Exceptions
  • 3. Introduction • Due to design errors or coding errors, our programs may fail in unexpected ways during execution. An exception is a condition that is caused by run time error in the program. The purpose of the exception handling mechanism is to provide a means to detect and report an “ecxceptional circumstances” .
  • 4. Error • An error may produce an incorrect output or may terminate the execution of the program abruptly or even may cause the system to crash. So it is our responsibility to detect and manage the error properly.
  • 5. Types of error • Runtime Errors: occur while the program is running if the environment detects an operation that is impossible to carry out. • Logic Errors: occur when a program doesn't perform the way it was intended • Syntax Errors: Arise because the rules of the language have not been followed. They are detected by the compiler.
  • 6. Example of Run Time error Class Error { public static void main(String args[]) { int a=10; int b=5; int c=5; int x=a/(b+c); System.out.println("x=" +x); int y=a/(b-c); // Errorr division by zero System.out.println("y=" +y); } }
  • 7. Errors and Error Handling • Some typical causes of errors: – Memory errors (i.e. memory incorrectly allocated, memory leaks, “null pointer”) – File system errors (i.e. disk is full, disk has been removed) – Network errors (i.e. network is down, URL does not exist) – Calculation errors (i.e. divide by 0)
  • 8. Errors and Error Handling • More typical causes of errors: – Array errors (i.e. accessing element –1) – Conversion errors (i.e. convert ‘q’ to a number) – Can you think of some others?
  • 9. Errors and Error Handling • Exceptions – a better error handling – Exceptions are a mechanism that provides the best of both worlds. – Exceptions act similar to method return flags in that any method may raise and exception should it encounter an error. – Exceptions act like global error methods in that the exception mechanism is built into Java; exceptions are handled at many levels in a program, locally and/or globally.
  • 10. Exceptions • How do you handle exceptions? – To handle the exception, you write a “try-catch” block. To pass the exception “up the chain”, you declare a throws clause in your method or class declaration. – If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception to the parent class (remember, every class has Object as the ultimate parent)
  • 11. Coding Exceptions • Coding Exceptions • Try-Catch Mechanism – Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: – After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword.
  • 12. Standard Java Exceptions Throwable Exception Error Runtime Exception IO Exception
  • 13. Catching Exceptions • Wrap code to be checked in a try-block – checking occurs all the way down the execution stack • try-blocks can be nested – control resumes at most enclosed matching handler
  • 14. Coding Exceptions • Example – try { … normal program code } catch(Exception e) { … exception handling code }
  • 15. Coding Exceptions • Types of Exceptions – Examples: • public void myMethod throws Exception { • public void myMethod throws IOException { • try { … } catch (Exception e) { … } • try { … } catch (IOException ioe) { … }
  • 16. Code Examples • 1. Demonstration of an unchecked exception (NullPointerException) • 2. Demonstration of checked exceptions: – Passing a DivideByZeroException – Handling a DivideByZeroException
  • 17. Example class error2 { public static void main(String arg[]) { int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System.out.println(“Division by Zero”); } Y=a/(b-c); System.out.println(“y=“+y); } }
  • 18. In the previous program we cannot see the value of x just because of the error in the value of y, that is division by zero but when we use the try and catch blocks in exception handling then we can see the value of y which is correct and our program will display an error message shown in the try block.
  • 19. conclusion – Exceptions are a powerful error handling mechanism. – Exceptions in Java are built into the language. – Exceptions can be handled by the programmer (try-catch), or handled by the Java environment (throws). – Exception handling can only hide the errors. – It cannot correct the errors.
  翻译: