SlideShare a Scribd company logo
Exception Handling in Java
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.

More Related Content

PPT
exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik
PPT
exception-handling-in-java.ppt edfrgthyujki8ol9k8ij7uhygtrfdewd3r4tf5ghy67u8u...
PPT
exception-handling,try,catch,throw,throws,finally,errors-in-java.ppt
PPT
exception-handling-in-java.ppt
PPT
how to do exception-handling-in-java.ppt
PPT
Exception Handling ppt slide presentation
PPT
exception-handling-in-java programming.ppt
PPT
exceptionvdffhhhccvvvv-handling-in-java.ppt
exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik
exception-handling-in-java.ppt edfrgthyujki8ol9k8ij7uhygtrfdewd3r4tf5ghy67u8u...
exception-handling,try,catch,throw,throws,finally,errors-in-java.ppt
exception-handling-in-java.ppt
how to do exception-handling-in-java.ppt
Exception Handling ppt slide presentation
exception-handling-in-java programming.ppt
exceptionvdffhhhccvvvv-handling-in-java.ppt

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

PPTX
Exception handling in java
PPTX
Java Exceptions and Exception Handling
PPTX
Exception handling
PPT
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
PPTX
Exception handling
PPT
Exception Handling Exception Handling Exception Handling
PPTX
Interface andexceptions
PPTX
using Java Exception Handling in Java.pptx
PPTX
Exception handling, Stream Classes, Multithread Programming
PDF
Chapter 5 Exception Handling (1).pdf
PPT
Exception Handling in java masters of computer application
PPT
Unit 5 Java
DOCX
MODULE5_EXCEPTION HANDLING.docx
PDF
Exception handling
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
PPT
A36519192_21789_4_2018_Exception Handling.ppt
PDF
Java exception handling ppt
PDF
Java_Exception-CheatSheet_Edureka.pdf
PDF
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
Exception handling in java
Java Exceptions and Exception Handling
Exception handling
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
Exception handling
Exception Handling Exception Handling Exception Handling
Interface andexceptions
using Java Exception Handling in Java.pptx
Exception handling, Stream Classes, Multithread Programming
Chapter 5 Exception Handling (1).pdf
Exception Handling in java masters of computer application
Unit 5 Java
MODULE5_EXCEPTION HANDLING.docx
Exception handling
unit 4 msbte syallbus for sem 4 2024-2025
A36519192_21789_4_2018_Exception Handling.ppt
Java exception handling ppt
Java_Exception-CheatSheet_Edureka.pdf
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
Ad

More from thenmozhip8 (14)

PPTX
U5 SPC.pptx
PDF
Unit 4.pdf
PPTX
unit 3 ppt.pptx
PPT
U2.ppt
PPT
Unit 1 .ppt
DOCX
IR UNIT V.docx
PPTX
IRT Unit_4.pptx
DOCX
UNIT 3 IRT.docx
PPTX
IRT Unit_ 2.pptx
PPTX
IRT Unit_I.pptx
PPT
packages unit 5 .ppt
PPT
unit 4 .ppt
PPTX
Definning class.pptx unit 3
PPTX
unit 1 full ppt.pptx
U5 SPC.pptx
Unit 4.pdf
unit 3 ppt.pptx
U2.ppt
Unit 1 .ppt
IR UNIT V.docx
IRT Unit_4.pptx
UNIT 3 IRT.docx
IRT Unit_ 2.pptx
IRT Unit_I.pptx
packages unit 5 .ppt
unit 4 .ppt
Definning class.pptx unit 3
unit 1 full ppt.pptx
Ad

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Lecture Notes Electrical Wiring System Components
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT 4 Total Quality Management .pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Lecture Notes Electrical Wiring System Components
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CYBER-CRIMES AND SECURITY A guide to understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Foundation to blockchain - A guide to Blockchain Tech
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Geodesy 1.pptx...............................................
UNIT 4 Total Quality Management .pptx

exception-handling-in-java.ppt unit 2

  • 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.