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

Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
Savitribai Phule Pune University
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Conditional statements
Conditional statementsConditional statements
Conditional statementscherrybear2014
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
Atul Sehdev
 
Exception handling
Exception handlingException handling
Exception handling
Abhishek Pachisia
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
baabtra.com - No. 1 supplier of quality freshers
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
jyoti_lakhani
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
Mindfire Solutions
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
ravinderkaur165
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
parag
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
Future Programming
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 

What's hot (20)

Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
07 flow control
07   flow control07   flow control
07 flow control
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Exception handling
Exception handlingException handling
Exception handling
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
M C6java5
M C6java5M C6java5
M C6java5
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 

Similar to Java chapter 6

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
RDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
RDeepa9
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Exception handling
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
MaqdamYasir
 

Similar to Java chapter 6 (20)

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 

More from Abdii Rashid

Java chapter 7
Java chapter 7Java chapter 7
Java chapter 7
Abdii Rashid
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
Abdii Rashid
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
Abdii Rashid
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Chapter 7 graphs
Chapter 7 graphsChapter 7 graphs
Chapter 7 graphs
Abdii Rashid
 
Chapter 1 introduction haramaya
Chapter 1 introduction haramayaChapter 1 introduction haramaya
Chapter 1 introduction haramaya
Abdii Rashid
 

More from Abdii Rashid (8)

Java chapter 7
Java chapter 7Java chapter 7
Java chapter 7
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Chapter 7 graphs
Chapter 7 graphsChapter 7 graphs
Chapter 7 graphs
 
Chapter 1 introduction haramaya
Chapter 1 introduction haramayaChapter 1 introduction haramaya
Chapter 1 introduction haramaya
 

Recently uploaded

Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...
Open Access Research Paper
 
Celebrating World-environment-day-2024.pdf
Celebrating  World-environment-day-2024.pdfCelebrating  World-environment-day-2024.pdf
Celebrating World-environment-day-2024.pdf
rohankumarsinghrore1
 
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdfUNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
JulietMogola
 
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptxAGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
BanitaDsouza
 
Daan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like itDaan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like it
a0966109726
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
sairaanwer024
 
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for..."Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
MMariSelvam4
 
Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024
punit537210
 
Sustainable farming practices in India .pptx
Sustainable farming  practices in India .pptxSustainable farming  practices in India .pptx
Sustainable farming practices in India .pptx
chaitaliambole
 
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian AmazonAlert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
CIFOR-ICRAF
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
World Resources Institute (WRI)
 
International+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shopInternational+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shop
laozhuseo02
 
alhambra case study Islamic gardens part-2.pptx
alhambra case study Islamic gardens part-2.pptxalhambra case study Islamic gardens part-2.pptx
alhambra case study Islamic gardens part-2.pptx
CECOS University Peshawar, Pakistan
 
Sustainable Rain water harvesting in india.ppt
Sustainable Rain water harvesting in india.pptSustainable Rain water harvesting in india.ppt
Sustainable Rain water harvesting in india.ppt
chaitaliambole
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
yasmindemoraes1
 
growbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdfgrowbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdf
yadavakashagra
 
ppt on beauty of the nature by Palak.pptx
ppt on  beauty of the nature by Palak.pptxppt on  beauty of the nature by Palak.pptx
ppt on beauty of the nature by Palak.pptx
RaniJaiswal16
 
Navigating the complex landscape of AI governance
Navigating the complex landscape of AI governanceNavigating the complex landscape of AI governance
Navigating the complex landscape of AI governance
Piermenotti Mauro
 
Scope of political science habaushS.pptx
Scope of political science habaushS.pptxScope of political science habaushS.pptx
Scope of political science habaushS.pptx
Ni Ca
 
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business VenturesWillie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
greendigital
 

Recently uploaded (20)

Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...Characterization and the Kinetics of drying at the drying oven and with micro...
Characterization and the Kinetics of drying at the drying oven and with micro...
 
Celebrating World-environment-day-2024.pdf
Celebrating  World-environment-day-2024.pdfCelebrating  World-environment-day-2024.pdf
Celebrating World-environment-day-2024.pdf
 
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdfUNDERSTANDING WHAT GREEN WASHING IS!.pdf
UNDERSTANDING WHAT GREEN WASHING IS!.pdf
 
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptxAGRICULTURE Hydrophonic FERTILISER PPT.pptx
AGRICULTURE Hydrophonic FERTILISER PPT.pptx
 
Daan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like itDaan Park Hydrangea flower season I like it
Daan Park Hydrangea flower season I like it
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
 
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for..."Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
"Understanding the Carbon Cycle: Processes, Human Impacts, and Strategies for...
 
Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024Artificial Reefs by Kuddle Life Foundation - May 2024
Artificial Reefs by Kuddle Life Foundation - May 2024
 
Sustainable farming practices in India .pptx
Sustainable farming  practices in India .pptxSustainable farming  practices in India .pptx
Sustainable farming practices in India .pptx
 
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian AmazonAlert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
Alert-driven Community-based Forest monitoring: A case of the Peruvian Amazon
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
 
International+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shopInternational+e-Commerce+Platform-www.cfye-commerce.shop
International+e-Commerce+Platform-www.cfye-commerce.shop
 
alhambra case study Islamic gardens part-2.pptx
alhambra case study Islamic gardens part-2.pptxalhambra case study Islamic gardens part-2.pptx
alhambra case study Islamic gardens part-2.pptx
 
Sustainable Rain water harvesting in india.ppt
Sustainable Rain water harvesting in india.pptSustainable Rain water harvesting in india.ppt
Sustainable Rain water harvesting in india.ppt
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
 
growbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdfgrowbilliontrees.com-Trees for Granddaughter (1).pdf
growbilliontrees.com-Trees for Granddaughter (1).pdf
 
ppt on beauty of the nature by Palak.pptx
ppt on  beauty of the nature by Palak.pptxppt on  beauty of the nature by Palak.pptx
ppt on beauty of the nature by Palak.pptx
 
Navigating the complex landscape of AI governance
Navigating the complex landscape of AI governanceNavigating the complex landscape of AI governance
Navigating the complex landscape of AI governance
 
Scope of political science habaushS.pptx
Scope of political science habaushS.pptxScope of political science habaushS.pptx
Scope of political science habaushS.pptx
 
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business VenturesWillie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
Willie Nelson Net Worth: A Journey Through Music, Movies, and Business Ventures
 

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