SlideShare a Scribd company logo
1 of 15
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

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 Exception Handling in Java - Overview, Syntax, Keywords, Examples

Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxNagaraju Pamarthi
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024kashyapneha2809
 
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 MultithreadingSakkaravarthiS1
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath 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 handlingKuntal Bhowmick
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
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 allHayomeTakele
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVAKunal Singh
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 

Similar to Exception Handling in Java - Overview, Syntax, Keywords, Examples (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
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java 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
 
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
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 

More from Abdii Rashid

object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examplesAbdii Rashid
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examplesAbdii 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 printAbdii Rashid
 
Chapter 1 introduction haramaya
Chapter 1 introduction haramayaChapter 1 introduction haramaya
Chapter 1 introduction haramayaAbdii 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

Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girlsPooja Nehwal
 
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012sapnasaifi408
 
History, principles and use for biopesticide risk assessment: Boet Glandorf a...
History, principles and use for biopesticide risk assessment: Boet Glandorf a...History, principles and use for biopesticide risk assessment: Boet Glandorf a...
History, principles and use for biopesticide risk assessment: Boet Glandorf a...OECD Environment
 
Spiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsSpiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsprasan26
 
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...Cluster TWEED
 
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130Suhani Kapoor
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesDr. Salem Baidas
 
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...ranjana rawat
 
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999Tina Ji
 

Recently uploaded (20)

Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
 
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service
(ANIKA) Call Girls Wagholi ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
Call Girls South Delhi Delhi reach out to us at ☎ 9711199012
 
9953056974 ,Low Rate Call Girls In Adarsh Nagar Delhi 24hrs Available
9953056974 ,Low Rate Call Girls In Adarsh Nagar  Delhi 24hrs Available9953056974 ,Low Rate Call Girls In Adarsh Nagar  Delhi 24hrs Available
9953056974 ,Low Rate Call Girls In Adarsh Nagar Delhi 24hrs Available
 
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Rajiv Chowk Delhi reach out to us at 🔝9953056974🔝
 
History, principles and use for biopesticide risk assessment: Boet Glandorf a...
History, principles and use for biopesticide risk assessment: Boet Glandorf a...History, principles and use for biopesticide risk assessment: Boet Glandorf a...
History, principles and use for biopesticide risk assessment: Boet Glandorf a...
 
E Waste Management
E Waste ManagementE Waste Management
E Waste Management
 
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
 
Spiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsSpiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnids
 
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
 
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
 
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service
(DIYA) Call Girls Sinhagad Road ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls In Yamuna Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Yamuna Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCeCall Girls In Yamuna Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Yamuna Vihar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
 
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
 
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Bhavna Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130
VIP Call Girls Service Tolichowki Hyderabad Call +91-8250192130
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and Challenges
 
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(PARI) Viman Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
 
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999
Call Girls In Faridabad(Ballabgarh) Book ☎ 8168257667, @4999
 
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls ServicesGandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
 

Exception Handling in Java - Overview, Syntax, Keywords, Examples

  • 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