SlideShare a Scribd company logo
Session-12
ExceptionHandling
● This module is intended to give an idea about
Exceptions and Errors.
● Covers different java constructs that are used to work
with exceptions .
● Introduces user defined exceptions.
Module Overview
2
 Define Exceptions in java.
 Understand Exception Hierarchy and classes.
 Identifying exceptions in a program.
 Handle Exceptions properly and take necessary
actions to work with exceptions.
 Understand different types of Exceptions.
 Identify the difference between Exceptions and
Errors.
 Understand predefined Exceptions.
 Create user defined exceptions.
Objectives
3
Exception:An exception is a problem that arises
during the execution of a program. An exception
can occur for many different reasons, including
the following:
 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
4
 Checked Exceptions: A checked exception is an
exception that is typically a user error or a problem
that cannot be foreseen by the programmer. For
example, if a file is to be opened, but the file cannot be
found, an exception occurs. These exceptions cannot
simply be ignored at the time of compilation.
 Runtime exceptions: A runtime exception is an
exception that occurs that probably could have been
avoided by the programmer. Runtime exceptions are
ignored at the time of compliation.
 Errors: Errors are typically ignored in the code
because programmer can rarely do anything about an
error. For example, if a stack overflow occurs, an error
will arise. They are also ignored at the time of
compilation.
Types Of Exceptions
5
 All exception classes are subtypes of the
java.lang.Exception class. The exception class is a
subclass of the Throwable class.
 Other than the exception class there is another
subclass called Error which is derived from the
Throwable class.
 Errors are conditions normally happen in case of
severe failures, which are not handled by the java
programs.
 Example : JVM is out of Memory.
Exception Hierarchy
6
Diagramatic representation of Exception Hierarchy.
Exception Hierarchy(Cont..)
7
Some important methods:
 public String getMessage()
Returns a detailed message about the exception
that has occurred. This message is initialized in
the Throwable constructor.
 public Throwable getCause()
Returns the cause of the exception as represented
by a Throwable object.
 public void printStackTrace()
Prints the result of toString() along with the stack
trace to System.err, the error output stream.
Throwable Class Methods
To work with exception java has provided following
clauses.
 Try/Catch: A method catches an exception using a
combination of the try and catch keywords. A
try/catch block is placed around the code that might
generate an exception. Code within a try/catch
block is referred to as protected code, and the
syntax for using try/catch looks like the following:
Try{
//Protected code
}catch(ExceptionName e1){
//Catch block
}
Working with Exceptions
9
Working with Exceptions(Cont..)
10
Catch(Specific):
 A catch statement involves declaring the type of
exception you are trying to catch.
 If an exception occurs in protected code, the catch
block (or blocks) that follows the try is checked.
 If the type of exception that occurred is listed in a
catch block, the exception is passed to the catch block.
 A try block can be followed by multiple catch blocks.
Throws Keyword
11
 Throws:If a method does not handle a checked exception,
the method must declare it using the throws keyword. The
throws keyword appears at the end of a method's signature.
 You can throw an exception, either a newly instantiated one
or an exception that you just caught, by using the throw
keyword.
Example:
public void deposit(double amount) throws IOException
{
// Method implementation
throw new IOException();
}
Throws: Amethod can declare that it throws more
than one exception, in which case the exceptions
are declared in a list separated by commas.
import java.io.*;
public class Bank{
public void withdraw(double amount) throws
RemoteException, InsufficientFundsException
{ // Method implementation }
//Remainder of class definition
}
Throws Keyword(Cont…)
12
The Finally Keyword
13
 The finally keyword is used to create a block of
code that follows a try block. A finally block of
code always executes, whether or not an
exception has occurred.
 Using a finally block allows you to run any
cleanup-type statements that you want to
execute, no matter what happens in the
protected code.
 A finally block appears at the end of the catch
blocks.
Syntax:
try{
//Protected code
}catch(ExceptionType1 e1){
//Catch block
}catch(ExceptionType2 e2){
//Catch block
}finally{
//The finally block always executes.
}
Finally(Cont..)
14
Important Points.
 A catch clause cannot exist without a try statement.
 It is not compulsory to have finally clauses when
ever a try/catch block is present.
 The try block cannot be present without either catch
clause or finally clause.
 Any code cannot be present in between the try, catch,
finally blocks.
 Exceptions can be created in Java.
 All exceptions must be a child of Throwable.
 If you want to write a checked exception that is
automatically enforced by the Handle or Declare Rule,
you need to extend the Exception class.
 If you want to write a runtime exception, you need to
extend the RuntimeException class.
User Defined Exceptions
16
Thank you

More Related Content

PDF
Java exceptions
PPTX
Exception handling
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
presentation-on-exception-handling 1.pptx
PPTX
Exception handling in Java
Java exceptions
Exception handling
Exception Handling,finally,catch,throw,throws,try.pptx
unit 4 msbte syallbus for sem 4 2024-2025
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
presentation-on-exception-handling-160611180456 (1).pptx
presentation-on-exception-handling 1.pptx
Exception handling in Java

Similar to Exception and ErrorHandling in Java .ppt (20)

PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
Java -Exception handlingunit-iv
PPTX
Interface andexceptions
PPT
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
PDF
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
PPT
Md07 exceptions&assertion
PPTX
UNIT 2.pptx
PPTX
Exception Handling in C#
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
PPT
Unit 5 Java
DOCX
Class notes(week 8) on exception handling
PPTX
Exception‐Handling in object oriented programming
PDF
summarizer16fev16_Exceptions
PDF
Exception handling basic
PPTX
UNIT-3.pptx Exception Handling and Multithreading
PPTX
Chapter 5
PPTX
Exceptions handling in java
PPS
Java Exception handling
PPTX
Exception handling in java
PPTX
UNIT III 2021R.pptx
presentation-on-exception-handling-160611180456 (1).pptx
Java -Exception handlingunit-iv
Interface andexceptions
Computer Object Oriented Programming - Chapter 4 - Excption Handling.ppt
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Md07 exceptions&assertion
UNIT 2.pptx
Exception Handling in C#
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Unit 5 Java
Class notes(week 8) on exception handling
Exception‐Handling in object oriented programming
summarizer16fev16_Exceptions
Exception handling basic
UNIT-3.pptx Exception Handling and Multithreading
Chapter 5
Exceptions handling in java
Java Exception handling
Exception handling in java
UNIT III 2021R.pptx
Ad

More from JyothiAmpally (19)

DOCX
node.js interview questions and answers.
PPT
Java Strings methods and operations.ppt
PPT
Arrays Basicfundamentaldatastructure.ppt
PPT
_Java__Expressions__and__FlowControl.ppt
PPT
Java-Variables_about_different_Scope.ppt
PPT
Java Operators explained _in __brief.ppt
PPT
UML to Object Oriented Mapping java .ppt
PPT
OOPS_AbstractClasses_explained__java.ppt
PPT
Java_Identifiers_keywords_data_types.ppt
PPT
Java_gui_with_AWT_and_its_components.ppt
PPT
1. Introduction to HTML5.ppt
PPT
01-basics-functions.ppt
PPT
03_A-OOPs_Interfaces.ppt
PPT
25-functions.ppt
PPT
03_A-OOPs_Interfaces.ppt
PPT
02-Java Technology Details.ppt
PPT
01-Java Introduction.ppt
PPT
01_What is Java.ppt
DOCX
Spring security
node.js interview questions and answers.
Java Strings methods and operations.ppt
Arrays Basicfundamentaldatastructure.ppt
_Java__Expressions__and__FlowControl.ppt
Java-Variables_about_different_Scope.ppt
Java Operators explained _in __brief.ppt
UML to Object Oriented Mapping java .ppt
OOPS_AbstractClasses_explained__java.ppt
Java_Identifiers_keywords_data_types.ppt
Java_gui_with_AWT_and_its_components.ppt
1. Introduction to HTML5.ppt
01-basics-functions.ppt
03_A-OOPs_Interfaces.ppt
25-functions.ppt
03_A-OOPs_Interfaces.ppt
02-Java Technology Details.ppt
01-Java Introduction.ppt
01_What is Java.ppt
Spring security
Ad

Recently uploaded (20)

PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
O7-L3 Supply Chain Operations - ICLT Program
Week 4 Term 3 Study Techniques revisited.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
NOI Hackathon - Summer Edition - GreenThumber.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
102 student loan defaulters named and shamed – Is someone you know on the list?
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Business Ethics Teaching Materials for college
Open folder Downloads.pdf yes yes ges yes
Revamp in MTO Odoo 18 Inventory - Odoo Slides
Open Quiz Monsoon Mind Game Prelims.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
How to Manage Starshipit in Odoo 18 - Odoo Slides

Exception and ErrorHandling in Java .ppt

  • 2. ● This module is intended to give an idea about Exceptions and Errors. ● Covers different java constructs that are used to work with exceptions . ● Introduces user defined exceptions. Module Overview 2
  • 3.  Define Exceptions in java.  Understand Exception Hierarchy and classes.  Identifying exceptions in a program.  Handle Exceptions properly and take necessary actions to work with exceptions.  Understand different types of Exceptions.  Identify the difference between Exceptions and Errors.  Understand predefined Exceptions.  Create user defined exceptions. Objectives 3
  • 4. Exception:An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:  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 4
  • 5.  Checked Exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.  Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. Runtime exceptions are ignored at the time of compliation.  Errors: Errors are typically ignored in the code because programmer can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. Types Of Exceptions 5
  • 6.  All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class.  Other than the exception class there is another subclass called Error which is derived from the Throwable class.  Errors are conditions normally happen in case of severe failures, which are not handled by the java programs.  Example : JVM is out of Memory. Exception Hierarchy 6
  • 7. Diagramatic representation of Exception Hierarchy. Exception Hierarchy(Cont..) 7
  • 8. Some important methods:  public String getMessage() Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.  public Throwable getCause() Returns the cause of the exception as represented by a Throwable object.  public void printStackTrace() Prints the result of toString() along with the stack trace to System.err, the error output stream. Throwable Class Methods
  • 9. To work with exception java has provided following clauses.  Try/Catch: A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: Try{ //Protected code }catch(ExceptionName e1){ //Catch block } Working with Exceptions 9
  • 10. Working with Exceptions(Cont..) 10 Catch(Specific):  A catch statement involves declaring the type of exception you are trying to catch.  If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked.  If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block.  A try block can be followed by multiple catch blocks.
  • 11. Throws Keyword 11  Throws:If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.  You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Example: public void deposit(double amount) throws IOException { // Method implementation throw new IOException(); }
  • 12. Throws: Amethod can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas. import java.io.*; public class Bank{ public void withdraw(double amount) throws RemoteException, InsufficientFundsException { // Method implementation } //Remainder of class definition } Throws Keyword(Cont…) 12
  • 13. The Finally Keyword 13  The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.  Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.  A finally block appears at the end of the catch blocks.
  • 14. Syntax: try{ //Protected code }catch(ExceptionType1 e1){ //Catch block }catch(ExceptionType2 e2){ //Catch block }finally{ //The finally block always executes. } Finally(Cont..) 14
  • 15. Important Points.  A catch clause cannot exist without a try statement.  It is not compulsory to have finally clauses when ever a try/catch block is present.  The try block cannot be present without either catch clause or finally clause.  Any code cannot be present in between the try, catch, finally blocks.
  • 16.  Exceptions can be created in Java.  All exceptions must be a child of Throwable.  If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.  If you want to write a runtime exception, you need to extend the RuntimeException class. User Defined Exceptions 16

Editor's Notes

  • #2: Instructor Notes:
  • #3: Instructor Notes: By the end of this module, participants should be able to: <Obj1> <Obj 2>
  • #4: Instructor Notes: <<Key Word>>: <<Definition>>
  • #5: Instructor Notes:
  • #6: Instructor Notes: <<Key Word>>: <<Definition>>
  • #10: Instructor Notes: <<Process flow steps>>
  • #11: Instructor Notes: <<Concept Description>>