SlideShare a Scribd company logo
Exception Handling in Java
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
“ecxception” .
Error
• An error may produce an incorrect output or
may terminate the execution of the program
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.ppt unit 2
PPT
exception-handling-in-java programming.ppt
PPT
Exception Handling ppt slide presentation
PPT
exception-handling,try,catch,throw,throws,finally,errors-in-java.ppt
PPT
how to do exception-handling-in-java.ppt
PPT
exception-handling-in-java.ppt
PPT
exception-handling-in-java.ppt edfrgthyujki8ol9k8ij7uhygtrfdewd3r4tf5ghy67u8u...
PPT
exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik
exception-handling-in-java.ppt unit 2
exception-handling-in-java programming.ppt
Exception Handling ppt slide presentation
exception-handling,try,catch,throw,throws,finally,errors-in-java.ppt
how to do exception-handling-in-java.ppt
exception-handling-in-java.ppt
exception-handling-in-java.ppt edfrgthyujki8ol9k8ij7uhygtrfdewd3r4tf5ghy67u8u...
exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik

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

PDF
Ch-1_5.pdf this is java tutorials for all
PPTX
Exceptions overview
PPT
exception-handling-in-java.ppt
PPT
A36519192_21789_4_2018_Exception Handling.ppt
PPTX
Java Exceptions and Exception Handling
PPTX
L14 exception handling
PPT
Exception Handling Exception Handling Exception Handling
PPT
Exception handling
PDF
Chapter 5 Exception Handling (1).pdf
PPT
Exception handling
PPTX
Exception handling in JAVA
PPTX
Week 4 - 5 Debugging Code and Analyzing Logic Errors.pptx
PPT
06 exceptions
PPTX
JAVA Presenttation topics Programs.pptx
PPTX
Exceptions in Java
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Java-Exception Handling Presentation. 2024
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
Ch-1_5.pdf this is java tutorials for all
Exceptions overview
exception-handling-in-java.ppt
A36519192_21789_4_2018_Exception Handling.ppt
Java Exceptions and Exception Handling
L14 exception handling
Exception Handling Exception Handling Exception Handling
Exception handling
Chapter 5 Exception Handling (1).pdf
Exception handling
Exception handling in JAVA
Week 4 - 5 Debugging Code and Analyzing Logic Errors.pptx
06 exceptions
JAVA Presenttation topics Programs.pptx
Exceptions in Java
unit 4 msbte syallbus for sem 4 2024-2025
Exception Handling In Java Presentation. 2024
Java-Exception Handling Presentation. 2024
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
Ad

Recently uploaded (20)

PPTX
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
PPTX
An introduction to AI in research and reference management
PDF
BRANDBOOK-Presidential Award Scheme-Kenya-2023
PDF
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
PPTX
YV PROFILE PROJECTS PROFILE PRES. DESIGN
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PPTX
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
PPTX
6- Architecture design complete (1).pptx
PDF
Quality Control Management for RMG, Level- 4, Certificate
PPTX
building Planning Overview for step wise design.pptx
PPTX
Media And Information Literacy for Grade 12
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PDF
Urban Design Final Project-Site Analysis
PPT
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PPT
pump pump is a mechanism that is used to transfer a liquid from one place to ...
PPT
UNIT I- Yarn, types, explanation, process
PPTX
rapid fire quiz in your house is your india.pptx
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PDF
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
BSCS lesson 3.pptxnbbjbb mnbkjbkbbkbbkjb
An introduction to AI in research and reference management
BRANDBOOK-Presidential Award Scheme-Kenya-2023
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
YV PROFILE PROJECTS PROFILE PRES. DESIGN
AD Bungalow Case studies Sem 2.pptxvwewev
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
6- Architecture design complete (1).pptx
Quality Control Management for RMG, Level- 4, Certificate
building Planning Overview for step wise design.pptx
Media And Information Literacy for Grade 12
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
Urban Design Final Project-Site Analysis
WHY_R12 Uaafafafpgradeaffafafafaffff.ppt
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
pump pump is a mechanism that is used to transfer a liquid from one place to ...
UNIT I- Yarn, types, explanation, process
rapid fire quiz in your house is your india.pptx
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
Ad

exceptionvdffhhhccvvvv-handling-in-java.ppt

  • 2. 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 “ecxception” .
  • 3. Error • An error may produce an incorrect output or may terminate the execution of the program even may cause the system to crash. So it is our responsibility to detect and manage the error properly.
  • 4. 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.
  • 5. 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); } }
  • 6. 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)
  • 7. 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?
  • 8. 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.
  • 9. 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)
  • 10. 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.
  • 11. Standard Java Exceptions Throwable Exception Error Runtime Exception IO Exception
  • 12. 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
  • 13. Coding Exceptions • Example – try { … normal program code } catch(Exception e) { … exception handling code }
  • 14. Coding Exceptions • Types of Exceptions – Examples: • public void myMethod throws Exception { • public void myMethod throws IOException { • try { … } catch (Exception e) { … } • try { … } catch (IOException ioe) { … }
  • 15. Code Examples • 1. Demonstration of an unchecked exception (NullPointerException) • 2. Demonstration of checked exceptions: – Passing a DivideByZeroException – Handling a DivideByZeroException
  • 16. 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); } }
  • 17. 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.
  • 18. 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.