SlideShare a Scribd company logo
Exception Handling
Syntax for exception class handling
Try catch finally block
Throw
Throws
User Defined Exceptions
Exception class hirearachy
What is Exception
• An exception is an event, which occurs during the execution of a program
that disrupts the normal flow of the program's instructions
Types of exceptions
• Checked exceptions
• Unchecked exceptions
Checked exceptions
compiler checks them during compilation to see whether the
programmer has handled them or not. If these exceptions are not
handled, it will give compilation error Eg:ClassNotFoundException
Unchecked exceptions
Runtime Exceptions are also known as Unchecked Exceptions as the compiler
do not check whether the programmer has handled them or not but it’s
the duty of the programmer to handle these exceptions and provide a safe
exit. Eg:ArithmeticException
Exception handling in java
Syntax for Exception Handling
Try
{ //code that may throw exception }
catch(Exception_class_Name ref)
{}
Try Block
• The try block contains a block of program statements within which an
exception might occur
• A try block is always followed by a catch block,
• A try block must followed by a Catch block or Finally block or both
Catch Block
• A catch block must be associated with a try block
• The corresponding catch block executes if an exception of a particular type
occurs within the try block.
Flow of try catch block
• If an exception occurs in try block then the control of execution is passed
to the catch block from try block.
• The exception is caught up by the corresponding catch block.
• A single try block can have multiple catch statements associated with it,
but each catch block can be defined for only one exception class.
• After the execution of all the try blocks, the code inside the finally block
executes. It is not mandatory to include a finally block at all
public static void main(String[] args)
{ System.out.println("enter you choice in 1 or 2");
int n; Scanner sc=new Scanner(System.in);
n=sc.nextInt(); //taking input
switch(n)
{ /* * Arithmetic exception */
case 1:{ try
{ int numarator=26,denominator=0;
int c=numarator/denominator;
System.out.println("eception raised");
}
catch(ArithmeticException e)
{ System.out.println("arithmetic exception");
}
}/*case*/break;
/** number format exception*/
case 2:{
try
{ int a=Integer.parseInt("X");
}//try
catch(NumberFormatException e)
{ System.out.println("Numberformat exception"); }
}/*case*/break;
}//switch
}//public
Try catch finally block• public static void main(String[] args)
{ System.out.println("enter you choice in 1:multiple catch blocksn 2:Example finally blockn3:try block
with finally block");
int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking input at runtime
switch(n)
{ /* * Arithmetic exception */
case 1:
{
case 2:{ try
{ int numarator=10,denominator=0;
int c=numarator/denominator;
System.out.println("the value of c is"+c);
}
catch(NumberFormatException e2)
{ System.out.println("arithmetic exception cought"); }
finally
{System.out.println("finally block excute every time"); }
}/*case*/break;
case 3:{ try
{ int numarator=10,denominator=0;
int c=numarator/denominator;
System.out.println("the value of c is"+c);
}
finally {System.out.println("finally block excute every time"); }
}/*case*/break;
}//switch
}//public
Throw
• The Java throw keyword is used to explicitly throw an exception
• We can throw either checked or unchecked exception in java by throw
keyword.
• Program execution stops on encountering throw statement, and the
closest catch statement is checked for matching type of exception.
Syntax
• throw ThrowableInstance
Creating Instance of Throwable Class
• Using a parameter in catch block.
• Creating instance with new operator.
public class Throw_Statement
{ static void age(int a)
{/** throwing an exception creating with new operator */
if(a<26)
{ throw new ArithmeticException("less than age");
}
else
System.out.println("Correct age");
}//age()
public static void main(String[] args)
{ age(10); }//main
}//class
####output####
Exception in thread "main" java.lang.ArithmeticException: less than age
Using a parameter in catch block
public class Throw_Statement
{ static void age(int a)
{ /* * throwing an exception with catch */
if(a<26)
{ try
{
throw new ArithmeticException("less than age");
}
catch(ArithmeticException e)
{
System.out.println("number is less than specifyed ::::"+e);
}
}
else
System.out.println("Correct age");
}//age()
public static void main(String[] args)
{ age(10); }//main
}//class
####output####
number is less than specifyed ::::java.lang.ArithmeticException: less than age
Throws
• The Java throws keyword is used to declare an exception
• It gives an information to the programmer that there may occur an
exception so it is better for the programmer to provide the exception
handling code
Syntax
• Return_type Method_name() throws Exception_Class_Name
• Throws key word example
User Defined Exceptions
• User defined exceptions in java are also known as Custom exceptions.
import java.io.IOException;
class My_Exception_Class extends Exception
{ String output;
public My_Exception_Class(String s)
{ output="the result is ="+s; }
/*public String toString()
{ return(output); }*/
}
public class Throw_Statement
{ public static void main(String[] args)
{ try
{
throw new My_Exception_Class("demo User Defined Exception");
}
catch(My_Exception_Class e)
{ System.out.println(e.output); }
}//main
}//class
####output###
the result is =demo User Defined Exception

More Related Content

PPT
Exception handling in java
PPTX
Exception handling in java
PPTX
7.error management and exception handling
PPSX
Exception Handling
PPTX
Exception handling in Java
PPTX
Chap2 exception handling
PPT
Exception Handling in JAVA
PPTX
Exceptionhandling
Exception handling in java
Exception handling in java
7.error management and exception handling
Exception Handling
Exception handling in Java
Chap2 exception handling
Exception Handling in JAVA
Exceptionhandling

What's hot (20)

PPT
Exception handling
PPTX
Exception handling in Java
ODP
Exception handling in java
PPTX
Java exception handling
PPTX
130410107010 exception handling
PPTX
Exception handling
PPTX
Exception handling in java
PPT
exception handling
PPT
PPTX
Exception handling
PPTX
Exception Handling in Java
PDF
Exception handling
PPTX
Java - Exception Handling
PPT
Exception handling
PPT
Exception handling
DOCX
Java Exception handling
PPTX
Exceptions in Java
PPT
Exception handling
PPT
Java: Exception
Exception handling
Exception handling in Java
Exception handling in java
Java exception handling
130410107010 exception handling
Exception handling
Exception handling in java
exception handling
Exception handling
Exception Handling in Java
Exception handling
Java - Exception Handling
Exception handling
Exception handling
Java Exception handling
Exceptions in Java
Exception handling
Java: Exception
Ad

Viewers also liked (20)

PDF
Yaazli International Spring Training
PDF
Yaazli International Hibernate Training
PDF
Java quick reference v2
DOC
Non ieee dot net projects list
PDF
Yaazli International AngularJS 5 Training
PPT
09events
PPTX
Core java online training
ODP
Toolbarexample
PDF
Yaazli International Web Project Workshop
PPT
02basics
PPTX
For Loops and Variables in Java
ODP
Singleton pattern
DOC
Non ieee java projects list
PPSX
DIWE - Using Extensions and Image Manipulation
PDF
Java Basic Operators
PPSX
DIWE - Multimedia Technologies
PDF
Yaazli International Java Training
PPTX
Java notes 1 - operators control-flow
PPT
java swing
Yaazli International Spring Training
Yaazli International Hibernate Training
Java quick reference v2
Non ieee dot net projects list
Yaazli International AngularJS 5 Training
09events
Core java online training
Toolbarexample
Yaazli International Web Project Workshop
02basics
For Loops and Variables in Java
Singleton pattern
Non ieee java projects list
DIWE - Using Extensions and Image Manipulation
Java Basic Operators
DIWE - Multimedia Technologies
Yaazli International Java Training
Java notes 1 - operators control-flow
java swing
Ad

Similar to Exception handling in java (20)

PPTX
Unit 4 exceptions and threads
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
presentation-on-exception-handling 1.pptx
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPT
Exceptionhandling
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPT
Exception Handling Exception Handling Exception Handling
PPTX
Java-Unit 3- Chap2 exception handling
PPTX
Interface andexceptions
PDF
Ch-1_5.pdf this is java tutorials for all
PPT
8.Exception handling latest(MB).ppt .
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
PPTX
Unit II Java & J2EE regarding Java application development
PPTX
Exception handling in java
PPTX
Exception Handling.pptx
PPT
Exception Handling
Unit 4 exceptions and threads
presentation-on-exception-handling-160611180456 (1).pptx
presentation-on-exception-handling 1.pptx
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exceptionhandling
Exception Handling,finally,catch,throw,throws,try.pptx
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
Exception Handling Exception Handling Exception Handling
Java-Unit 3- Chap2 exception handling
Interface andexceptions
Ch-1_5.pdf this is java tutorials for all
8.Exception handling latest(MB).ppt .
UNIT III 2021R.pptx
UNIT III 2021R.pptx
Unit II Java & J2EE regarding Java application development
Exception handling in java
Exception Handling.pptx
Exception Handling

More from yugandhar vadlamudi (13)

PPT
Object Relational model for SQLIite in android
DOCX
JButton in Java Swing example
PPTX
Collections framework in java
PPTX
Packaes & interfaces
DOCX
JMenu Creation in Java Swing
DOCX
Adding a action listener to button
PPTX
Dynamic method dispatch
PPTX
Operators in java
PPTX
Inheritance
PPTX
Control flow statements in java
PPTX
Closer look at classes
PPTX
java Applet Introduction
PPTX
Class introduction in java
Object Relational model for SQLIite in android
JButton in Java Swing example
Collections framework in java
Packaes & interfaces
JMenu Creation in Java Swing
Adding a action listener to button
Dynamic method dispatch
Operators in java
Inheritance
Control flow statements in java
Closer look at classes
java Applet Introduction
Class introduction in java

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharma ospi slides which help in ospi learning
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Week 4 Term 3 Study Techniques revisited.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
Cell Types and Its function , kingdom of life
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharma ospi slides which help in ospi learning

Exception handling in java

  • 1. Exception Handling Syntax for exception class handling Try catch finally block Throw Throws User Defined Exceptions Exception class hirearachy
  • 2. What is Exception • An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions Types of exceptions • Checked exceptions • Unchecked exceptions Checked exceptions compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled, it will give compilation error Eg:ClassNotFoundException Unchecked exceptions Runtime Exceptions are also known as Unchecked Exceptions as the compiler do not check whether the programmer has handled them or not but it’s the duty of the programmer to handle these exceptions and provide a safe exit. Eg:ArithmeticException
  • 4. Syntax for Exception Handling Try { //code that may throw exception } catch(Exception_class_Name ref) {} Try Block • The try block contains a block of program statements within which an exception might occur • A try block is always followed by a catch block, • A try block must followed by a Catch block or Finally block or both Catch Block • A catch block must be associated with a try block • The corresponding catch block executes if an exception of a particular type occurs within the try block.
  • 5. Flow of try catch block • If an exception occurs in try block then the control of execution is passed to the catch block from try block. • The exception is caught up by the corresponding catch block. • A single try block can have multiple catch statements associated with it, but each catch block can be defined for only one exception class. • After the execution of all the try blocks, the code inside the finally block executes. It is not mandatory to include a finally block at all
  • 6. public static void main(String[] args) { System.out.println("enter you choice in 1 or 2"); int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking input switch(n) { /* * Arithmetic exception */ case 1:{ try { int numarator=26,denominator=0; int c=numarator/denominator; System.out.println("eception raised"); } catch(ArithmeticException e) { System.out.println("arithmetic exception"); } }/*case*/break; /** number format exception*/ case 2:{ try { int a=Integer.parseInt("X"); }//try catch(NumberFormatException e) { System.out.println("Numberformat exception"); } }/*case*/break; }//switch }//public
  • 7. Try catch finally block• public static void main(String[] args) { System.out.println("enter you choice in 1:multiple catch blocksn 2:Example finally blockn3:try block with finally block"); int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking input at runtime switch(n) { /* * Arithmetic exception */ case 1: { case 2:{ try { int numarator=10,denominator=0; int c=numarator/denominator; System.out.println("the value of c is"+c); } catch(NumberFormatException e2) { System.out.println("arithmetic exception cought"); } finally {System.out.println("finally block excute every time"); } }/*case*/break; case 3:{ try { int numarator=10,denominator=0; int c=numarator/denominator; System.out.println("the value of c is"+c); } finally {System.out.println("finally block excute every time"); } }/*case*/break; }//switch }//public
  • 8. Throw • The Java throw keyword is used to explicitly throw an exception • We can throw either checked or unchecked exception in java by throw keyword. • Program execution stops on encountering throw statement, and the closest catch statement is checked for matching type of exception. Syntax • throw ThrowableInstance Creating Instance of Throwable Class • Using a parameter in catch block. • Creating instance with new operator.
  • 9. public class Throw_Statement { static void age(int a) {/** throwing an exception creating with new operator */ if(a<26) { throw new ArithmeticException("less than age"); } else System.out.println("Correct age"); }//age() public static void main(String[] args) { age(10); }//main }//class ####output#### Exception in thread "main" java.lang.ArithmeticException: less than age
  • 10. Using a parameter in catch block public class Throw_Statement { static void age(int a) { /* * throwing an exception with catch */ if(a<26) { try { throw new ArithmeticException("less than age"); } catch(ArithmeticException e) { System.out.println("number is less than specifyed ::::"+e); } } else System.out.println("Correct age"); }//age() public static void main(String[] args) { age(10); }//main }//class ####output#### number is less than specifyed ::::java.lang.ArithmeticException: less than age
  • 11. Throws • The Java throws keyword is used to declare an exception • It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code Syntax • Return_type Method_name() throws Exception_Class_Name • Throws key word example
  • 12. User Defined Exceptions • User defined exceptions in java are also known as Custom exceptions. import java.io.IOException; class My_Exception_Class extends Exception { String output; public My_Exception_Class(String s) { output="the result is ="+s; } /*public String toString() { return(output); }*/ } public class Throw_Statement { public static void main(String[] args) { try { throw new My_Exception_Class("demo User Defined Exception"); } catch(My_Exception_Class e) { System.out.println(e.output); } }//main }//class ####output### the result is =demo User Defined Exception