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.

More Related Content

Similar to exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik (20)

PPTX
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
PPTX
Exception handling
Minal Maniar
 
PPT
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
kushal797779
 
PDF
Exception handling
Muthukumaran Subramanian
 
PPTX
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
PDF
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PPT
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
PPT
Exception Handling in java masters of computer application
xidileh999
 
DOCX
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
PPTX
Z blue exception
Narayana Swamy
 
PPTX
Exception handling in java
Kavitha713564
 
PDF
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
PDF
Java_Exception-CheatSheet_Edureka.pdf
Furkan Furkan
 
PPTX
Chap2 exception handling
raksharao
 
PPTX
Java-Unit 3- Chap2 exception handling
raksharao
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPTX
Driver Magician Full Crack + Keygen Free Download [Latest]
hamzapc291
 
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
Exception handling
Minal Maniar
 
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
kushal797779
 
Exception handling
Muthukumaran Subramanian
 
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
Exception Handling in java masters of computer application
xidileh999
 
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Z blue exception
Narayana Swamy
 
Exception handling in java
Kavitha713564
 
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
Java_Exception-CheatSheet_Edureka.pdf
Furkan Furkan
 
Chap2 exception handling
raksharao
 
Java-Unit 3- Chap2 exception handling
raksharao
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Driver Magician Full Crack + Keygen Free Download [Latest]
hamzapc291
 

Recently uploaded (20)

PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 
PPTX
site survey architecture student B.arch.
sri02032006
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
Structural Functiona theory this important for the theorist
cagumaydanny26
 
PDF
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 
site survey architecture student B.arch.
sri02032006
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
Structural Functiona theory this important for the theorist
cagumaydanny26
 
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Thermal runway and thermal stability.pptx
godow93766
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
Ad

exception-handling-in-java (1).pptwsedrftgyhujiqawsedrftgyhujik

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