SlideShare a Scribd company logo
{
PRESENTATION ON
Exception Handling
What is an exception?
*An exception is an error condition that
changes the normal flow of control in a
program
*When an Exception occurs the normal flow
of the program is disrupted and the
program/Application terminates
abnormally, which is not recommended,
therefore these exceptions are to be handled
Why Exception Occurs?
An exception can occur for many different
reasons, below given are some scenarios where
exception occurs.
>>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 Handling,finally,catch,throw,throws,try.pptx
Exception Hierarchy
Exception Handling,finally,catch,throw,throws,try.pptx
Exception Has two Main
classes :
1. Checked exceptions : known as
compile time exceptions.
Programmer should take care of
(handle) these exceptions
2. Unchecked exceptions : Known as
Runtime Exceptions.
These include programming bugs, such
as logic error also.
{
Example 1
public class TryCatchExample1 {
public static void main(String[] args) {
int data=50/0; //may throw exception
System.out.println("rest of the code");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is not executed (in
such case, the rest of the code statement is not printed).
{
Solution by exception handling
Let's see the solution of the above problem by a java try-catch block.
public class TryCatchExample2 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is executed, i.e.,
the rest of the code statement is printed
java.lang.ArithmeticException: / by zero rest of the code
Keyword Description
try The "try" keyword is used to specify a block
where we should place an exception code. It
means we can't use try block alone. The try block
must be followed by either catch or finally.
catch The "catch" block is used to handle the exception.
It must be preceded by try block which means we
can't use catch block alone. It can be followed by
finally block later.
finally The "finally" block is used to execute the
necessary code of the program. It is executed
whether an exception is handled or not.
throw The "throw" keyword is used to throw an
exception.
throws The "throws" keyword is used to declare
exceptions. It specifies that there may occur an
exception in the method. It doesn't throw an
exception. It is always used with method
signature.
Checked exceptions
import java.io.File;
import java.io.FileReader;
public class FilenotFound_Demo {
public static void main(String args[]){
File file=new File("E://file.txt");
FileReader fr = new FileReader(file);
}
} Output: C:>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8:
error: unreported exception
FileNotFoundException; must be caught or
declared to be thrown
FileReader fr = new FileReader(file);
Example
Unchecked exceptions
Example
public class Unchecked_Demo {
public static void main(String
args[]){
int num[]={1,2,3,4};
System.out.println(num[5]);
}
} Output:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5 at
Exceptions.Unchecked_Demo.main(Unchecked_D
emo.java:8
Exception Handling Terms
1.Try – used to enclose a segment of code that may
produce a exception
2.Catch – placed directly after the try block to handle one
or more exception types
3.Throw – to generate an exception or to describe an
instance of an exception
4.Finally – optional statement used after a try-catch block
to run a segment of code regardless if a exception is
generated
Try – Catch Block
Try – used to enclose a segment of code that
may produce a exception
Catch – placed directly after the try block to
handle one or more exception types
try {
statements;
}
catch(Exception ex) {
perform operations before exits;
throw ex;
}
Multiple catch statements
try {
<code segment that may
throw an exception..>
} catch (IOException e) {
System.out.println(e.getMessage()
);
} catch (FileNotFoundException e){
System.out.println(“FileNotFound!”);
}
Nested try-catch block
try {
statement 1;
try {
statement 2;
statement 3;
}
catch(Exception e) { }
}
catch(Exception e) { }
By using Throw
THROW-generate an exception or to describe an instance
of an exception
Define a class:
public class EmptyStackException extends Exception {
}
Here is how you use the class:
public class Stack {
public Object Pop() throws EmptyStackException
{
if (Empty()) throw new EmptyStackException();
...
}
}
Note that you must use new to create an exception
object; you cannot just throw an exception.
Example
static class Exception2{
static int sum(int num1, int num2){
if (num1 == 0)
throw new ArithmeticException("First parameter is not
valid");
else
System.out.println("Both parameters are correct!!");
return num1+num2; }
public static void main(String args[]){
int res=sum(1,12);
System.out.println(res);
System.out.println("Continue Next statements");
}
}
The finally
try {
statements;
}
catch(TheExceptionex) {
handling ex;
}
finally {
finalStatements;
}
Example
public static void main(String[] arg){
try{
int i = 10/0;
} catch(Exception ex){
System.out.println("Inside 1st catch Block");
} finally {
System.out.println("Inside 1st finally block");
}
try{
int i = 10/10;
} catch(Exception ex){
System.out.println("Inside 2nd catch Block");
} finally {
System.out.println("Inside 2nd finally block");
}
} Inside 1st catch Block
Inside 1st finally block
Inside 2nd finally block
Thanks All

More Related Content

PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
presentation-on-exception-handling 1.pptx
PPTX
Presentation on-exception-handling
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
Interface andexceptions
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPT
Exceptionhandling
presentation-on-exception-handling-160611180456 (1).pptx
presentation-on-exception-handling 1.pptx
Presentation on-exception-handling
presentation-on-exception-handling-160611180456 (1).pptx
Interface andexceptions
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
Exceptionhandling

Similar to Exception Handling,finally,catch,throw,throws,try.pptx (20)

PPTX
Chap2 exception handling
PPTX
Java-Unit 3- Chap2 exception handling
PDF
Java unit 11
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPTX
Exception Handling.pptx
PPT
Exception Handling Exception Handling Exception Handling
PPTX
java exception.pptx
PPTX
Exception‐Handling in object oriented programming
PPTX
Exception handling in java
PPTX
Java exception handling
PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PPTX
Unit-4 Java ppt for BCA Students Madras Univ
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
PPT
Exception
Chap2 exception handling
Java-Unit 3- Chap2 exception handling
Java unit 11
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling.pptx
Exception Handling Exception Handling Exception Handling
java exception.pptx
Exception‐Handling in object oriented programming
Exception handling in java
Java exception handling
PACKAGES, INTERFACES AND EXCEPTION HANDLING
Unit-4 Java ppt for BCA Students Madras Univ
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Ad

More from ArunPatrick2 (19)

PPT
Introduction to HTML table,width,height.ppt
PPTX
introduction to java Multithreading presentation.pptx
PPT
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
PPTX
collectionsframework210616084411 (1).pptx
PPT
Interfaces implements,presentation in java.ppt
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPTX
InterfaceAbstractClass presentation.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
java package java package in java packages
PPT
Interfaces in java.. introduction, classes, objects
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Data Analytics overview,kDD process,mining Techniques.pptx
PPTX
Data warehouse-complete-1-100227093028-phpapp01.pptx
PPTX
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
PPTX
Difference between Abstract class and Interface.pptx
PPT
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
PPTX
InterfaceAbstractClass,interfaces,final keyword,.pptx
Introduction to HTML table,width,height.ppt
introduction to java Multithreading presentation.pptx
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
collectionsframework210616084411 (1).pptx
Interfaces implements,presentation in java.ppt
multithreading,thread and processinjava-210302183809.pptx
InterfaceAbstractClass presentation.pptx
unit3 Exception Handling multithreadingppt.pptx
unit3multithreadingppt-copy-180122162204.pptx
java package java package in java packages
Interfaces in java.. introduction, classes, objects
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
Inheritance,single,multiple.access rulepptx
Data Analytics overview,kDD process,mining Techniques.pptx
Data warehouse-complete-1-100227093028-phpapp01.pptx
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
Difference between Abstract class and Interface.pptx
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
InterfaceAbstractClass,interfaces,final keyword,.pptx
Ad

Recently uploaded (20)

PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Classroom Observation Tools for Teachers
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Weekly quiz Compilation Jan -July 25.pdf
History, Philosophy and sociology of education (1).pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Yogi Goddess Pres Conference Studio Updates
Classroom Observation Tools for Teachers
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Orientation - ARALprogram of Deped to the Parents.pptx
Cell Structure & Organelles in detailed.
Weekly quiz Compilation Jan -July 25.pdf

Exception Handling,finally,catch,throw,throws,try.pptx

  • 2. What is an exception? *An exception is an error condition that changes the normal flow of control in a program *When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled
  • 3. Why Exception Occurs? An exception can occur for many different reasons, below given are some scenarios where exception occurs. >>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.
  • 7. Exception Has two Main classes : 1. Checked exceptions : known as compile time exceptions. Programmer should take care of (handle) these exceptions 2. Unchecked exceptions : Known as Runtime Exceptions. These include programming bugs, such as logic error also.
  • 8. { Example 1 public class TryCatchExample1 { public static void main(String[] args) { int data=50/0; //may throw exception System.out.println("rest of the code"); } } Output: Exception in thread "main" java.lang.ArithmeticException: / by zero As displayed in the above example, the rest of the code is not executed (in such case, the rest of the code statement is not printed).
  • 9. { Solution by exception handling Let's see the solution of the above problem by a java try-catch block. public class TryCatchExample2 { public static void main(String[] args) { try { int data=50/0; //may throw exception } //handling the exception catch(ArithmeticException e) { System.out.println(e); } System.out.println("rest of the code"); } } Output: Exception in thread "main" java.lang.ArithmeticException: / by zero As displayed in the above example, the rest of the code is executed, i.e., the rest of the code statement is printed java.lang.ArithmeticException: / by zero rest of the code
  • 10. Keyword Description try The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by either catch or finally. catch The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later. finally The "finally" block is used to execute the necessary code of the program. It is executed whether an exception is handled or not. throw The "throw" keyword is used to throw an exception. throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature.
  • 12. import java.io.File; import java.io.FileReader; public class FilenotFound_Demo { public static void main(String args[]){ File file=new File("E://file.txt"); FileReader fr = new FileReader(file); } } Output: C:>javac FilenotFound_Demo.java FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr = new FileReader(file); Example
  • 14. Example public class Unchecked_Demo { public static void main(String args[]){ int num[]={1,2,3,4}; System.out.println(num[5]); } } Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Exceptions.Unchecked_Demo.main(Unchecked_D emo.java:8
  • 15. Exception Handling Terms 1.Try – used to enclose a segment of code that may produce a exception 2.Catch – placed directly after the try block to handle one or more exception types 3.Throw – to generate an exception or to describe an instance of an exception 4.Finally – optional statement used after a try-catch block to run a segment of code regardless if a exception is generated
  • 16. Try – Catch Block Try – used to enclose a segment of code that may produce a exception Catch – placed directly after the try block to handle one or more exception types try { statements; } catch(Exception ex) { perform operations before exits; throw ex; }
  • 17. Multiple catch statements try { <code segment that may throw an exception..> } catch (IOException e) { System.out.println(e.getMessage() ); } catch (FileNotFoundException e){ System.out.println(“FileNotFound!”); }
  • 18. Nested try-catch block try { statement 1; try { statement 2; statement 3; } catch(Exception e) { } } catch(Exception e) { }
  • 19. By using Throw THROW-generate an exception or to describe an instance of an exception Define a class: public class EmptyStackException extends Exception { } Here is how you use the class: public class Stack { public Object Pop() throws EmptyStackException { if (Empty()) throw new EmptyStackException(); ... } } Note that you must use new to create an exception object; you cannot just throw an exception.
  • 20. Example static class Exception2{ static int sum(int num1, int num2){ if (num1 == 0) throw new ArithmeticException("First parameter is not valid"); else System.out.println("Both parameters are correct!!"); return num1+num2; } public static void main(String args[]){ int res=sum(1,12); System.out.println(res); System.out.println("Continue Next statements"); } }
  • 21. The finally try { statements; } catch(TheExceptionex) { handling ex; } finally { finalStatements; }
  • 22. Example public static void main(String[] arg){ try{ int i = 10/0; } catch(Exception ex){ System.out.println("Inside 1st catch Block"); } finally { System.out.println("Inside 1st finally block"); } try{ int i = 10/10; } catch(Exception ex){ System.out.println("Inside 2nd catch Block"); } finally { System.out.println("Inside 2nd finally block"); } } Inside 1st catch Block Inside 1st finally block Inside 2nd finally block