SlideShare a Scribd company logo
Chapter 6
Exception Handling
Outline
• Exception handling overview
• Syntax
Exception handling overview
• The exception handling in java is one of the
powerful mechanism to handle the runtime
errors so that normal flow of the application
can be maintained.
• In java, exception is an event that disrupts the
normal flow of the program. It is an object
which is thrown at runtime.
Cont…
• Exception Handling is a mechanism to handle
runtime errors such as ClassNotFound, IO,
SQL, Remote etc.
• The core advantage of exception handling is to
maintain the normal flow of the application.
Hierarchy of Java Exception classes
Types of Exception
• There are mainly two types of exceptions:
checked and unchecked where error is
considered as unchecked exception.
• According to the sun microsystem says there
are three types of exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
Cont…
Checked Exception
• The classes that extend Throwable class except RuntimeException
and Error are known as checked exceptions e.g.IOException,
SQLException etc. Checked exceptions are checked at compile-
time.
Unchecked Exception
• The classes that extend RuntimeException are known as
unchecked exceptions e.g. ArithmeticException,
NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time rather
they are checked at runtime.
Error
• Error is irrecoverable e.g. OutOfMemoryError,
VirtualMachineError, AssertionError etc.
Common scenarios where exceptions may occur
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an
ArithmeticException.
int a=50/0;//ArithmeticException
2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any
operation by the variable occurs a
NullPointerException.
String s=null;
System.out.println(s.length());//NullPointerException
Common scenarios where exceptions may occur
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur
NumberFormatException. Suppose I have a string variable
that have characters, converting this variable into digit will
occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
4) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would
result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Java Exception Handling Keywords
• There are 5 keywords used in java exception
handling.
Try
Catch
Finally
Throw
throws
Java try block
• Java try block is used to enclose the code that
might throw an exception. It must be used within
the method.
• Java try block must be followed by either catch or
finally block.
Syntax of java try-catch
try{
//code that may throw exception
}catch(Exception_class_Name ref){}
Cont…
Syntax of try-catch-finally block
try{
//code that may throw exception
}
catch(Exception_class_Name ref){
}
finally{}
Example
Problem without exception handling
• Let's try to understand the problem if we
don't use try-catch block.
public class Testtrycatch1{
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
Example …
Solution by exception handling
• Let's see the solution of above problem by java try-catch block.
public class Testtrycatch2{
public static void main(String args[]){
try{
int data=50/0;
}catch(ArithmeticException e){
System.out.println(e);}
System.out.println("rest of the code..."); } }
• Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
Internal working of java try-catch block

More Related Content

What's hot (20)

PPT
Control statements in java programmng
Savitribai Phule Pune University
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPSX
Control Structures in Visual Basic
Tushar Jain
 
PPTX
Conditional statements
cherrybear2014
 
PPTX
07 flow control
dhrubo kayal
 
PPT
Control structures ii
Ahmad Idrees
 
PPTX
Control statements in java
Madishetty Prathibha
 
PDF
itft-Decision making and branching in java
Atul Sehdev
 
PPTX
Exception handling
Abhishek Pachisia
 
PPTX
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
java programming- control statements
jyoti_lakhani
 
PPT
M C6java5
mbruggen
 
PPTX
Control statements in Java
Jin Castor
 
PPT
Understanding Exception Handling in .Net
Mindfire Solutions
 
PDF
Java conditional statements
Kuppusamy P
 
PPTX
Exception handling in java
ravinderkaur165
 
ODP
Exception Handling In Java
parag
 
PPT
Java control flow statements
Future Programming
 
PPTX
Exception handling chapter15
Kumar
 
PPTX
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Control statements in java programmng
Savitribai Phule Pune University
 
Java Decision Control
Jayfee Ramos
 
Control Structures in Visual Basic
Tushar Jain
 
Conditional statements
cherrybear2014
 
07 flow control
dhrubo kayal
 
Control structures ii
Ahmad Idrees
 
Control statements in java
Madishetty Prathibha
 
itft-Decision making and branching in java
Atul Sehdev
 
Exception handling
Abhishek Pachisia
 
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
java programming- control statements
jyoti_lakhani
 
M C6java5
mbruggen
 
Control statements in Java
Jin Castor
 
Understanding Exception Handling in .Net
Mindfire Solutions
 
Java conditional statements
Kuppusamy P
 
Exception handling in java
ravinderkaur165
 
Exception Handling In Java
parag
 
Java control flow statements
Future Programming
 
Exception handling chapter15
Kumar
 
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 

Similar to Java chapter 6 (20)

PPTX
Exceptionhandling
Nuha Noor
 
PPTX
Exception Handling.pptx
primevideos176
 
PPTX
Exception handling in java.pptx
Nagaraju Pamarthi
 
PPT
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
PPTX
Lec-01 Exception Handling in Java_javatpoint.pptx
MdNazmulHasanRuhan1
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPT
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
PPTX
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
PPTX
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
JeevaR43
 
PPTX
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
PPT
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
PPTX
Exceptions overview
Bharath K
 
PPTX
UNIT 2.pptx
EduclentMegasoftel
 
PDF
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
Exceptionhandling
Nuha Noor
 
Exception Handling.pptx
primevideos176
 
Exception handling in java.pptx
Nagaraju Pamarthi
 
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
Lec-01 Exception Handling in Java_javatpoint.pptx
MdNazmulHasanRuhan1
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
OBJECT ORIENTED PROGRAMMING STRUCU1.pptx
JeevaR43
 
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Exceptions overview
Bharath K
 
UNIT 2.pptx
EduclentMegasoftel
 
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exception handling in java
pooja kumari
 
Exception handling in java
pooja kumari
 
UNIT III 2021R.pptx
RDeepa9
 
UNIT III 2021R.pptx
RDeepa9
 
Ad

More from Abdii Rashid (8)

PPTX
Java chapter 7
Abdii Rashid
 
PPTX
Java chapter 5
Abdii Rashid
 
PPTX
Java chapter 4
Abdii Rashid
 
PPTX
object oriented programming examples
Abdii Rashid
 
PPTX
object oriented programming examples
Abdii Rashid
 
PDF
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
PDF
Chapter 7 graphs
Abdii Rashid
 
PPTX
Chapter 1 introduction haramaya
Abdii Rashid
 
Java chapter 7
Abdii Rashid
 
Java chapter 5
Abdii Rashid
 
Java chapter 4
Abdii Rashid
 
object oriented programming examples
Abdii Rashid
 
object oriented programming examples
Abdii Rashid
 
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Chapter 7 graphs
Abdii Rashid
 
Chapter 1 introduction haramaya
Abdii Rashid
 
Ad

Recently uploaded (20)

DOCX
16-7-25 Daily Report OSH LO-I OK Ouccupational.docx
fahadafzalg181
 
PDF
Generalized levelized cost as a metric for explaining model behavior of linea...
IEA-ETSAP
 
PDF
Dr. Philippe Smith - A Climate Data Analyst
Dr. Philippe Smith
 
PPTX
Control Panels glbkjh ubyiuyb bybuu.pptx
shnawy
 
PPTX
Ed2 (1).pptxHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
23eg506b03
 
DOCX
5466219wedrftttttttttttttttttttttt64-1st-Quarter.docx
GellaBenson1
 
PPTX
chapter 08 air and climate2025july.pptx
Ali Al-naqa
 
PPTX
Use_of_PPEs_Storyboard safety bdgnfgnccx
Arun Phoke
 
PDF
Application of remote sensing techniques and environmental factors in separat...
Open Access Research Paper
 
DOCX
Epoxy Coated Tanks for Industrial Water Stores Water for Industrial Processes...
AllenLin596164
 
PPTX
Bio-pesticides or Botanical pesticides..
Laxman Khatal
 
PDF
Reactivity in the Periodic Table Chemistry Presentation in a Blue Bold Styl_2...
BrianDelaSerna
 
PPTX
animal-research-and-alternatives methods
biggdaad011
 
PDF
Conflict Minerals and Responsible Sourcing.pdf
admin797519
 
PDF
Through the Landscape – Landscape Leadership Camp (LLC)
Global Landscapes Forum (GLF)
 
PPTX
Year 11 Work Experience Program (WEP).pptx
CheahJinShuen
 
PDF
Regenerating Land, Reimaiging Leadership – Landscape Leadership Camp (LLC)
Global Landscapes Forum (GLF)
 
PDF
The Water Freedom System- A Game-Changer for Our Global Water Crisis.pdf
Mr. Obaid Khan
 
PPTX
AI_Crop_Disease_Prediction_Presentation.pptx
ajha73032
 
PPTX
solar tree presentation partha.pptx for engineering students
sahuadityaranjan3
 
16-7-25 Daily Report OSH LO-I OK Ouccupational.docx
fahadafzalg181
 
Generalized levelized cost as a metric for explaining model behavior of linea...
IEA-ETSAP
 
Dr. Philippe Smith - A Climate Data Analyst
Dr. Philippe Smith
 
Control Panels glbkjh ubyiuyb bybuu.pptx
shnawy
 
Ed2 (1).pptxHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
23eg506b03
 
5466219wedrftttttttttttttttttttttt64-1st-Quarter.docx
GellaBenson1
 
chapter 08 air and climate2025july.pptx
Ali Al-naqa
 
Use_of_PPEs_Storyboard safety bdgnfgnccx
Arun Phoke
 
Application of remote sensing techniques and environmental factors in separat...
Open Access Research Paper
 
Epoxy Coated Tanks for Industrial Water Stores Water for Industrial Processes...
AllenLin596164
 
Bio-pesticides or Botanical pesticides..
Laxman Khatal
 
Reactivity in the Periodic Table Chemistry Presentation in a Blue Bold Styl_2...
BrianDelaSerna
 
animal-research-and-alternatives methods
biggdaad011
 
Conflict Minerals and Responsible Sourcing.pdf
admin797519
 
Through the Landscape – Landscape Leadership Camp (LLC)
Global Landscapes Forum (GLF)
 
Year 11 Work Experience Program (WEP).pptx
CheahJinShuen
 
Regenerating Land, Reimaiging Leadership – Landscape Leadership Camp (LLC)
Global Landscapes Forum (GLF)
 
The Water Freedom System- A Game-Changer for Our Global Water Crisis.pdf
Mr. Obaid Khan
 
AI_Crop_Disease_Prediction_Presentation.pptx
ajha73032
 
solar tree presentation partha.pptx for engineering students
sahuadityaranjan3
 

Java chapter 6

  • 2. Outline • Exception handling overview • Syntax
  • 3. Exception handling overview • The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. • In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
  • 4. Cont… • Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc. • The core advantage of exception handling is to maintain the normal flow of the application.
  • 5. Hierarchy of Java Exception classes
  • 6. Types of Exception • There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. • According to the sun microsystem says there are three types of exceptions: 1. Checked Exception 2. Unchecked Exception 3. Error
  • 7. Cont… Checked Exception • The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile- time. Unchecked Exception • The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. Error • Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 8. Common scenarios where exceptions may occur 1) Scenario where ArithmeticException occurs If we divide any number by zero, there occurs an ArithmeticException. int a=50/0;//ArithmeticException 2) Scenario where NullPointerException occurs If we have null value in any variable, performing any operation by the variable occurs a NullPointerException. String s=null; System.out.println(s.length());//NullPointerException
  • 9. Common scenarios where exceptions may occur 3) Scenario where NumberFormatException occurs The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException. String s="abc"; int i=Integer.parseInt(s);//NumberFormatException 4) Scenario where ArrayIndexOutOfBoundsException occurs If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below: int a[]=new int[5]; a[10]=50; //ArrayIndexOutOfBoundsException
  • 10. Java Exception Handling Keywords • There are 5 keywords used in java exception handling. Try Catch Finally Throw throws
  • 11. Java try block • Java try block is used to enclose the code that might throw an exception. It must be used within the method. • Java try block must be followed by either catch or finally block. Syntax of java try-catch try{ //code that may throw exception }catch(Exception_class_Name ref){}
  • 12. Cont… Syntax of try-catch-finally block try{ //code that may throw exception } catch(Exception_class_Name ref){ } finally{}
  • 13. Example Problem without exception handling • Let's try to understand the problem if we don't use try-catch block. public class Testtrycatch1{ 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
  • 14. Example … Solution by exception handling • Let's see the solution of above problem by java try-catch block. public class Testtrycatch2{ public static void main(String args[]){ try{ int data=50/0; }catch(ArithmeticException e){ System.out.println(e);} System.out.println("rest of the code..."); } } • Output: Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
  • 15. Internal working of java try-catch block