SlideShare a Scribd company logo
IADCS Diploma Course Exception Handling U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
Introduction to Exception  Is a special type of error   It occurs at runtime in a code sequence   Abnormal conditions that occur while executing the program cause exceptions   If these conditions are not dealt with, then the execution can be terminated abruptly
Purpose of Exception handling Minimize the chances of a system crash, or abrupt program termination For example,  In an I/O operation in a file. If the data type conversion is not properly done, an exception occurs, and the program aborts, without closing the file. This may damage the file, and the resources allocated to the file may not return to the system
Handling Exceptions  When an exception occurs, an object that represents that exception is created  This object is then passed to the method where the exception has occurred The object contains detailed information about the exception. This information can be retrieved and processed  The ’throwable’ class that Java provides is the superclass of the Exception class, which is, in turn, the superclass of individual exceptions
Structure of Exception Object Error Throwable Exception (LinkageError, ThreadDeath, VirtualMachineError) ClassNotFoundException IOException(EOF,FileNotFound) NoSuchFieldException NoSuchMethodException RuntimeException (Arithemetic,IllegalArgument, IndexOutOfBounds,NullPointer)
Structure of Exception(Cont:) Object Throwable Error Exception Runtime Exception uncheck check uncheck
How exception work? //to show exception class UNO{ public static void main(String args[]){ int num=Integer.parseInt(args[0]); System.out.println("Here is "+num); } }
Exception handling Model Is also known as the ‘catch and throw’ model   When an error occurs, an ‘exception’ is thrown, and caught in a block K eywords to handle exceptions try catch throw throws  finally
Structure of the exception handling model  Syntax try { ….  } catch(Exception e1) { …. } catch(Exception e2) { …. } catch(Exception eN) { …. } finally { …. }
Advantages of ‘Catch and Throw’ Model   The programmer has to deal with an error condition only where necessary. It need not be dealt with at every level  An error message can be provided in the exception-handler
‘ try’ and ‘catch’ Blocks   Is used to implement the ‘catch and throw’ model of exception handling A ‘try’ block consists of a set of executable statements   A method, which may throw an exception, can also be included in the ‘try’ block One or more ‘catch’ blocks can follow a ‘try’ block   These ‘catch’ blocks catch exceptions thrown in the ‘try’ block
try’ and ‘catch’ Blocks (Contd…) To catch any type of exception, specify the exception type as ‘Exception’   catch(Exception e) When the type of exception being thrown is not known, the class ‘Exception’  can be used  to catch that exception The error passes through the ‘try catch’ block, until it encounters a ‘catch’ that matches it, or the program terminates
Example of catch and throw model //Number Format Exception Example class Try_Example{ public static void main(String args[]){ try{ int n=Integer.parseInt(args[0]); System.out.println("N is "+n); } catch(Exception e){ System.out.println("E is "+e); } } }
Example(cont) //Arithemetic Exception Example class Try_Example{ public static void main(String args[]){ int demo=0; try{ System.out.println(20/demo); } catch(Exception e){ System.out.println("E is "+e); } } }
Multiple Catch Blocks   Multiple ‘catch()’ blocks process various exception types separately Example try {  doFileProcessing();  displayResults();  }  catch(LookupException e)    {  handleLookupException(e);  }  catch(Exception e)      { System.err.println(“Error:”+e.printStackTrace());  }
Multiple Catch Blocks  (Contd…) When nested ‘try’ blocks are used, the inner ‘try’ block is executed first Any exception thrown in the inner ‘try’ block is caught in the following ‘catch’ blocks If a matching ‘catch’ block is not found, then ‘catch’ blocks of the outer ‘try’ blocks are inspected  Otherwise, the Java Runtime Environment handles the exception
‘finally’ Block Takes care of all the cleanup work when an exception occurs   Can be used in conjunction with a ‘try’ block   Contains statements that either return resources to the system, or print messages Closing a file  Closing a result set (used in Database programming) Closing the connection established with the database
‘ finally’ Block (Contd…) Example try  { doSomethingThatMightThrowAnException( ); }  finally  { cleanup( ); }
‘ finally’ Block (Contd…) Is optional  Is placed after the last ‘catch’ block  The ‘finally’ block is guaranteed to run, whether or not an exception occurs
Example of Finally Block //Finally Block Example class Finally_Example{ public static void main(String args[]){ int demo=0; try{ System.out.println(20/demo); } catch(Exception e){ System.out.println("E is "+e); } finally{ System.out.println("Finally Executed"); } } }
User-defined Exceptions with ‘throw’ and ‘throws’ statements   Exceptions are thrown with the help of the ‘throw’ keyword  The ‘throw’ keyword indicates that an exception has occurred  The operand of throw is an object of a class, which is derived from the class ‘Throwable’  Example of the ‘throw’ statement  try{ if  (flag < 0)   {   throw new MyException( ) ;  // user-defined  } }
User-defined Exceptions with ‘throw’ and ‘throws’ statements (Contd…) A single method may throw more than one exception   Example of the ‘throw’ keyword to handle multiple exceptions   public class Example { public void exceptionExample( ) throws ExException,  LookupException  { try {  // statements  } catch(ExException exmp) { …. } catch(LookupException lkpex) { …. }   }  }
User-defined Exceptions with ‘throw’ and ‘throws’ statements (Contd…) The ‘Exception’ class implements the ‘Throwable’ interface, and provides some useful features for dealing with exceptions   Advantage of subclassing the Exception class is that the new exception type can be caught separately from other Throwable types
User Defined Exception Example public class ExTest{ public static void main(String args[]){ int num=Integer.parseInt(args[0]); try{ if(num<0) throw new MyExceptionTest(); } catch(MyExceptionTest e) { System.out.println(e); } } } public class MyExceptionTest extends ArithmeticException{ MyExceptionTest(){ super(&quot;You have passed Illegal Operation&quot;); } }
Multi catch example public class Pre { public static void main (String argv[]) { try { double d = Double.valueOf(argv[0]).doubleValue();   System.out.println (d); } catch (ArrayIndexOutOfBoundsException e) { System.out.println (&quot;An argument is required.&quot;); return; } catch (NumberFormatException e) { System.out.println (&quot;The argument must be a real number.&quot;); return; } } }
List of Exceptions RuntimeException   ArithmeticException IllegalAccessException   IllegalArgumentException ArrayIndexOutOfBoundsException NullPointerException   SecurityException   ClassNotFoundException   NumberFormatException AWTException IOException FileNotFoundException EOFException   NoSuchMethodException   InterruptedException
Thank You!

More Related Content

PPTX
Exception handling
PhD Research Scholar
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
PPT
Java exception
Arati Gadgil
 
PPTX
Java Array String
Manish Tiwari
 
PPS
Java Exception handling
kamal kotecha
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPTX
Python Summer Internship
Atul Kumar
 
Exception handling
PhD Research Scholar
 
Presentation on-exception-handling
Nahian Ahmed
 
Java exception
Arati Gadgil
 
Java Array String
Manish Tiwari
 
Java Exception handling
kamal kotecha
 
Exception Handling in Java
lalithambiga kamaraj
 
Exception Handling in JAVA
SURIT DATTA
 
Python Summer Internship
Atul Kumar
 

What's hot (20)

PPTX
Operator Overloading & Function Overloading
Meghaj Mallick
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Python Exception Handling
Megha V
 
PPTX
C++ Overview PPT
Thooyavan Venkatachalam
 
PDF
Date and Time Module in Python | Edureka
Edureka!
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
Strings in Java
Abhilash Nair
 
ODP
Exception handling in python
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Abstract class in c++
Sujan Mia
 
DOCX
DAA Lab File C Programs
Kandarp Tiwari
 
PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PPTX
Functions in python
colorsof
 
PPTX
Loops in Python
Arockia Abins
 
PPTX
String in java
Ideal Eyes Business College
 
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
Edureka!
 
PPTX
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
PPTX
Types of Constructor in C++
Bhavik Vashi
 
PPTX
Need of object oriented programming
Amar Jukuntla
 
Operator Overloading & Function Overloading
Meghaj Mallick
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
Virtual base class
Tech_MX
 
Python Exception Handling
Megha V
 
C++ Overview PPT
Thooyavan Venkatachalam
 
Date and Time Module in Python | Edureka
Edureka!
 
07. Virtual Functions
Haresh Jaiswal
 
Strings in Java
Abhilash Nair
 
Abstract class in c++
Sujan Mia
 
DAA Lab File C Programs
Kandarp Tiwari
 
Python basics
RANAALIMAJEEDRAJPUT
 
Functions in python
colorsof
 
Loops in Python
Arockia Abins
 
Enumeration in Java Explained | Java Tutorial | Edureka
Edureka!
 
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Types of Constructor in C++
Bhavik Vashi
 
Need of object oriented programming
Amar Jukuntla
 
Ad

Similar to Exception Handling (20)

PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPT
Chap12
Terry Yoast
 
PPTX
Java-Unit 3- Chap2 exception handling
raksharao
 
PPTX
Chap2 exception handling
raksharao
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
java exception.pptx
SukhpreetSingh519414
 
PPTX
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 
PPT
Java: Exception
Tareq Hasan
 
PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
PPTX
Exception handling in java
yugandhar vadlamudi
 
PPT
9781439035665 ppt ch11
Terry Yoast
 
PPT
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
PPT
Unit 5 Java
arnold 7490
 
PPTX
presentation-on-exception-handling 1.pptx
ArunPatrickK1
 
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
ArunPatrick2
 
PPTX
java.pptx
sujatha629799
 
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
PPT
Exception
Harry Potter
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Chap12
Terry Yoast
 
Java-Unit 3- Chap2 exception handling
raksharao
 
Chap2 exception handling
raksharao
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Java exception handling
BHUVIJAYAVELU
 
java exception.pptx
SukhpreetSingh519414
 
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 
Java: Exception
Tareq Hasan
 
PACKAGES, INTERFACES AND EXCEPTION HANDLING
mohanrajm63
 
Exception handling in java
yugandhar vadlamudi
 
9781439035665 ppt ch11
Terry Yoast
 
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Unit 5 Java
arnold 7490
 
presentation-on-exception-handling 1.pptx
ArunPatrickK1
 
presentation-on-exception-handling-160611180456 (1).pptx
ArunPatrick2
 
java.pptx
sujatha629799
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Exception
Harry Potter
 
Ad

More from backdoor (20)

PPT
Java Database Connectivity
backdoor
 
PPT
Distributed Programming using RMI
backdoor
 
PPT
Programming Server side with Sevlet
backdoor
 
PPT
Distributed Programming using RMI
backdoor
 
PPT
Client Side Programming with Applet
backdoor
 
PPT
Java Network Programming
backdoor
 
PPT
Windows Programming with Swing
backdoor
 
PPT
Windows Programming with AWT
backdoor
 
PPT
Multithreading
backdoor
 
PPT
Object and Classes in Java
backdoor
 
PPT
IO and serialization
backdoor
 
PPT
Java Intro
backdoor
 
PPT
Object Oriented Programming with Java
backdoor
 
PPT
AWT Program output
backdoor
 
PPT
Net Man
backdoor
 
PPT
Data Security
backdoor
 
PPT
Ne Course Part One
backdoor
 
PPT
Ne Course Part Two
backdoor
 
PPT
Net Sec
backdoor
 
PDF
Security Policy Checklist
backdoor
 
Java Database Connectivity
backdoor
 
Distributed Programming using RMI
backdoor
 
Programming Server side with Sevlet
backdoor
 
Distributed Programming using RMI
backdoor
 
Client Side Programming with Applet
backdoor
 
Java Network Programming
backdoor
 
Windows Programming with Swing
backdoor
 
Windows Programming with AWT
backdoor
 
Multithreading
backdoor
 
Object and Classes in Java
backdoor
 
IO and serialization
backdoor
 
Java Intro
backdoor
 
Object Oriented Programming with Java
backdoor
 
AWT Program output
backdoor
 
Net Man
backdoor
 
Data Security
backdoor
 
Ne Course Part One
backdoor
 
Ne Course Part Two
backdoor
 
Net Sec
backdoor
 
Security Policy Checklist
backdoor
 

Recently uploaded (20)

DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Software Development Methodologies in 2025
KodekX
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
GYTPOL If You Give a Hacker a Host
linda296484
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 

Exception Handling

  • 1. IADCS Diploma Course Exception Handling U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  • 2. Introduction to Exception Is a special type of error It occurs at runtime in a code sequence Abnormal conditions that occur while executing the program cause exceptions If these conditions are not dealt with, then the execution can be terminated abruptly
  • 3. Purpose of Exception handling Minimize the chances of a system crash, or abrupt program termination For example, In an I/O operation in a file. If the data type conversion is not properly done, an exception occurs, and the program aborts, without closing the file. This may damage the file, and the resources allocated to the file may not return to the system
  • 4. Handling Exceptions When an exception occurs, an object that represents that exception is created This object is then passed to the method where the exception has occurred The object contains detailed information about the exception. This information can be retrieved and processed The ’throwable’ class that Java provides is the superclass of the Exception class, which is, in turn, the superclass of individual exceptions
  • 5. Structure of Exception Object Error Throwable Exception (LinkageError, ThreadDeath, VirtualMachineError) ClassNotFoundException IOException(EOF,FileNotFound) NoSuchFieldException NoSuchMethodException RuntimeException (Arithemetic,IllegalArgument, IndexOutOfBounds,NullPointer)
  • 6. Structure of Exception(Cont:) Object Throwable Error Exception Runtime Exception uncheck check uncheck
  • 7. How exception work? //to show exception class UNO{ public static void main(String args[]){ int num=Integer.parseInt(args[0]); System.out.println(&quot;Here is &quot;+num); } }
  • 8. Exception handling Model Is also known as the ‘catch and throw’ model When an error occurs, an ‘exception’ is thrown, and caught in a block K eywords to handle exceptions try catch throw throws finally
  • 9. Structure of the exception handling model Syntax try { …. } catch(Exception e1) { …. } catch(Exception e2) { …. } catch(Exception eN) { …. } finally { …. }
  • 10. Advantages of ‘Catch and Throw’ Model The programmer has to deal with an error condition only where necessary. It need not be dealt with at every level An error message can be provided in the exception-handler
  • 11. ‘ try’ and ‘catch’ Blocks Is used to implement the ‘catch and throw’ model of exception handling A ‘try’ block consists of a set of executable statements A method, which may throw an exception, can also be included in the ‘try’ block One or more ‘catch’ blocks can follow a ‘try’ block These ‘catch’ blocks catch exceptions thrown in the ‘try’ block
  • 12. try’ and ‘catch’ Blocks (Contd…) To catch any type of exception, specify the exception type as ‘Exception’ catch(Exception e) When the type of exception being thrown is not known, the class ‘Exception’ can be used to catch that exception The error passes through the ‘try catch’ block, until it encounters a ‘catch’ that matches it, or the program terminates
  • 13. Example of catch and throw model //Number Format Exception Example class Try_Example{ public static void main(String args[]){ try{ int n=Integer.parseInt(args[0]); System.out.println(&quot;N is &quot;+n); } catch(Exception e){ System.out.println(&quot;E is &quot;+e); } } }
  • 14. Example(cont) //Arithemetic Exception Example class Try_Example{ public static void main(String args[]){ int demo=0; try{ System.out.println(20/demo); } catch(Exception e){ System.out.println(&quot;E is &quot;+e); } } }
  • 15. Multiple Catch Blocks Multiple ‘catch()’ blocks process various exception types separately Example try { doFileProcessing(); displayResults(); } catch(LookupException e) { handleLookupException(e); } catch(Exception e) { System.err.println(“Error:”+e.printStackTrace()); }
  • 16. Multiple Catch Blocks (Contd…) When nested ‘try’ blocks are used, the inner ‘try’ block is executed first Any exception thrown in the inner ‘try’ block is caught in the following ‘catch’ blocks If a matching ‘catch’ block is not found, then ‘catch’ blocks of the outer ‘try’ blocks are inspected Otherwise, the Java Runtime Environment handles the exception
  • 17. ‘finally’ Block Takes care of all the cleanup work when an exception occurs Can be used in conjunction with a ‘try’ block Contains statements that either return resources to the system, or print messages Closing a file Closing a result set (used in Database programming) Closing the connection established with the database
  • 18. ‘ finally’ Block (Contd…) Example try { doSomethingThatMightThrowAnException( ); } finally { cleanup( ); }
  • 19. ‘ finally’ Block (Contd…) Is optional Is placed after the last ‘catch’ block The ‘finally’ block is guaranteed to run, whether or not an exception occurs
  • 20. Example of Finally Block //Finally Block Example class Finally_Example{ public static void main(String args[]){ int demo=0; try{ System.out.println(20/demo); } catch(Exception e){ System.out.println(&quot;E is &quot;+e); } finally{ System.out.println(&quot;Finally Executed&quot;); } } }
  • 21. User-defined Exceptions with ‘throw’ and ‘throws’ statements Exceptions are thrown with the help of the ‘throw’ keyword The ‘throw’ keyword indicates that an exception has occurred The operand of throw is an object of a class, which is derived from the class ‘Throwable’ Example of the ‘throw’ statement try{ if (flag < 0) { throw new MyException( ) ; // user-defined } }
  • 22. User-defined Exceptions with ‘throw’ and ‘throws’ statements (Contd…) A single method may throw more than one exception   Example of the ‘throw’ keyword to handle multiple exceptions public class Example { public void exceptionExample( ) throws ExException, LookupException { try { // statements } catch(ExException exmp) { …. } catch(LookupException lkpex) { …. } } }
  • 23. User-defined Exceptions with ‘throw’ and ‘throws’ statements (Contd…) The ‘Exception’ class implements the ‘Throwable’ interface, and provides some useful features for dealing with exceptions Advantage of subclassing the Exception class is that the new exception type can be caught separately from other Throwable types
  • 24. User Defined Exception Example public class ExTest{ public static void main(String args[]){ int num=Integer.parseInt(args[0]); try{ if(num<0) throw new MyExceptionTest(); } catch(MyExceptionTest e) { System.out.println(e); } } } public class MyExceptionTest extends ArithmeticException{ MyExceptionTest(){ super(&quot;You have passed Illegal Operation&quot;); } }
  • 25. Multi catch example public class Pre { public static void main (String argv[]) { try { double d = Double.valueOf(argv[0]).doubleValue(); System.out.println (d); } catch (ArrayIndexOutOfBoundsException e) { System.out.println (&quot;An argument is required.&quot;); return; } catch (NumberFormatException e) { System.out.println (&quot;The argument must be a real number.&quot;); return; } } }
  • 26. List of Exceptions RuntimeException ArithmeticException IllegalAccessException IllegalArgumentException ArrayIndexOutOfBoundsException NullPointerException SecurityException ClassNotFoundException NumberFormatException AWTException IOException FileNotFoundException EOFException NoSuchMethodException InterruptedException