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.
presentation-on-exception-handling 1.pptx
Exception Hierarchy
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.
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
Exception Handling,finally,catch,throw,throws,try.pptx
PPTX
Presentation on-exception-handling
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPT
Exceptionhandling
PPTX
Java-Unit 3- Chap2 exception handling
PPTX
Chap2 exception handling
Exception Handling,finally,catch,throw,throws,try.pptx
Presentation on-exception-handling
presentation-on-exception-handling-160611180456 (1).pptx
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
Exceptionhandling
Java-Unit 3- Chap2 exception handling
Chap2 exception handling

Similar to presentation-on-exception-handling 1.pptx (20)

PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PPTX
Unit 4 exceptions and threads
PPTX
130410107010 exception handling
PPTX
java exception.pptx
PPTX
Interface andexceptions
PPTX
Java exception handling
DOCX
Java Exception handling
PDF
Java unit 11
PPT
Exception Handling Exception Handling Exception Handling
DOCX
Exception handling
PPTX
Exception‐Handling in object oriented programming
PPTX
Exception handling in java
PPTX
Exception Handling.pptx
PDF
Java unit3
PPTX
Chapter class 11 exception and errors-9.pptx
PPTX
Exceptions handling in java
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPTX
java.pptx
PPT
Exception
PPT
Exception
PACKAGES, INTERFACES AND EXCEPTION HANDLING
Unit 4 exceptions and threads
130410107010 exception handling
java exception.pptx
Interface andexceptions
Java exception handling
Java Exception handling
Java unit 11
Exception Handling Exception Handling Exception Handling
Exception handling
Exception‐Handling in object oriented programming
Exception handling in java
Exception Handling.pptx
Java unit3
Chapter class 11 exception and errors-9.pptx
Exceptions handling in java
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
java.pptx
Exception
Exception
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Trump Administration's workforce development strategy
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Cell Types and Its function , kingdom of life
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
Empowerment Technology for Senior High School Guide
PDF
IGGE1 Understanding the Self1234567891011
PPTX
History, Philosophy and sociology of education (1).pptx
Final Presentation General Medicine 03-08-2024.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Unit 4 Skeletal System.ppt.pptxopresentatiom
What if we spent less time fighting change, and more time building what’s rig...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Trump Administration's workforce development strategy
A systematic review of self-coping strategies used by university students to ...
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Cell Types and Its function , kingdom of life
Digestion and Absorption of Carbohydrates, Proteina and Fats
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
Empowerment Technology for Senior High School Guide
IGGE1 Understanding the Self1234567891011
History, Philosophy and sociology of education (1).pptx
Ad

presentation-on-exception-handling 1.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.
  • 6. 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.
  • 7. 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.
  • 9. 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
  • 11. 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
  • 12. 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
  • 13. 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; }
  • 14. 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!”); }
  • 15. Nested try-catch block try { statement 1; try { statement 2; statement 3; } catch(Exception e) { } } catch(Exception e) { }
  • 16. 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.
  • 17. 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"); } }
  • 18. The finally try { statements; } catch(TheExceptionex) { handling ex; } finally { finalStatements; }
  • 19. 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