SlideShare a Scribd company logo
1 of 42
EXCEPTION HANDLING
IN JAVA
Tips and tools for creating and presenting wide format
slides
 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.
Types of Exception
 Checked Exception
 Unchecked Exception
 Error
 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
 Scenario where ArithmeticException occurs
 Scenario where NullPointerException occurs
 Scenario where NumberFormatException
occurs
 Scenario where
ArrayIndexOutOfBoundsException occurs
 Java Exception Handling Keywords
 Java try-catch:
 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)
 {}
 Syntax of try-finally block:
 Try
 {
 //code that may throw exception
 }
 Finally
 {}
 Java catch block:
 Java catch block is used to handle the
Exception. It must be used after the try block
only.
 You can use multiple catch block with a single
try.
 Problem without exception handling
 public class Testtrycatch1
 {
 public static void main(String args[])
 {
 int data=50/0;//may throw exception
 System.out.println("rest of the code...");
 }
 }
 Solution by exception handling
 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...");
 } }
 Internal working of java try-catch block
 Java catch multiple exceptions
 public class TestMultipleCatchBlock{
 public static void main(String args[]){
 try
 {
 int a[]=new int[5];
 a[5]=30/0;
 }
 catch(ArithmeticException e){System.out.println("task1 is complet
ed");}
 catch(ArrayIndexOutOfBoundsException e){System.out.println("ta
sk 2 completed");}
 catch(Exception e){System.out.println("common task completed")
;}

 System.out.println("rest of the code...");
 } }
 Rule: At a time only one Exception is occured
and at a time only one catch block is executed.
 Rule: All catch blocks must be ordered from
most specific to most general i.e. catch for
ArithmeticException must come before catch
for Exception .
 class TestMultipleCatchBlock1{
 public static void main(String args[]){
 try
 {
 int a[]=new int[5];
 a[15]=30;
 }
 catch(Exception e){System.out.println("common task completed
");}
 catch(ArithmeticException e){System.out.println("task1 is compl
eted");}
 catch(ArrayIndexOutOfBoundsException e){System.out.println("
task 2 completed");}
 System.out.println("rest of the code...");
 Java Nested try block
 The try block within a try block is known as
nested try block in java.
 Why use nested try block
 Sometimes a situation may arise where a part
of a block may cause one error and the entire
block itself may cause another error. In such
cases, exception handlers have to be nested.
 try
 {
 statement 1;
 try
 {
 statement 1;
 }
 catch(Exception e)
 {
 }
 }
 catch(Exception e)
 {
 }
 Java finally block:
 Java finally block is a block that is used to
execute important code such as closing
connection, stream etc.
 Java finally block is always executed whether
exception is handled or not.
 Java finally block follows try or catch block.
 Note: If you don't handle exception, before
terminating the program, JVM executes finally
block(if any).
 Why use java finally
 Finally block in java can be used to put
"cleanup" code such as closing a file, closing
connection etc.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
System.out.println(data); }
catch(NullPointerException e){System.out.println(e)
;}
finally{System.out.println("finally block is always ex
ecuted");}
System.out.println("rest of the code...");
} }
 Rule: For each try block there can be zero
or more catch blocks, but only one finally
block.
 Java throw keyword:
 The Java throw keyword is used to explicitly
throw an exception.
 We can throw either checked or uncheked
exception in java by throw keyword.
 The syntax of java throw keyword is given
below.
 throw exception;
 Example of throw IOException.
 throw new IOException("sorry device error);
 Java throw keyword example
 In this example, we have created the validate
method that takes integer value as a
parameter. If the age is less than 18, we are
throwing the ArithmeticException otherwise
print a message welcome to vote.
 public class TestThrow1{
 static void validate(int age){
 if(age<18)
 throw new ArithmeticException("not valid");
 else
 System.out.println("welcome to vote");
 }
 public static void main(String args[]){
 validate(13);
 System.out.println("rest of the code..."); } }
 Java throws keyword
 The Java throws keyword is used to declare
an exception. It gives an information to the
programmer that there may occur an exception
so it is better for the programmer to provide
the exception handling code so that normal
flow can be maintained.
 Exception Handling is mainly used to handle
the checked exceptions. If there occurs any
unchecked exception such as
NullPointerException, it is programmers fault
that he is not performing check up before the
code being used.
 Syntax of java throws:
 returnType method() throws exceptionClassNam
e
 {
 //method code
 }
 Java throws example

More Related Content

What's hot

What's hot (20)

Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Files in java
Files in javaFiles in java
Files in java
 
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 - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java I/O
Java I/OJava I/O
Java I/O
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Java threads
Java threadsJava threads
Java threads
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 

Similar to Exception handling in java

Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxNagaraju Pamarthi
 
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
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024kashyapneha2809
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 
1.12 Exception Handing.pptx
1.12 Exception Handing.pptx1.12 Exception Handing.pptx
1.12 Exception Handing.pptxYashiUPADHYAY6
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javagopalrajput11
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception HandlingPrabhdeep Singh
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in javajunnubabu
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1 Hitesh-Java
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javachauhankapil
 

Similar to Exception handling in java (20)

Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
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
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.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
 
1.12 Exception Handing.pptx
1.12 Exception Handing.pptx1.12 Exception Handing.pptx
1.12 Exception Handing.pptx
 
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
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Exceptions
ExceptionsExceptions
Exceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 

More from Lovely Professional University

The HyperText Markup Language or HTML is the standard markup language
The HyperText Markup Language or HTML is the standard markup languageThe HyperText Markup Language or HTML is the standard markup language
The HyperText Markup Language or HTML is the standard markup languageLovely Professional University
 

More from Lovely Professional University (20)

The HyperText Markup Language or HTML is the standard markup language
The HyperText Markup Language or HTML is the standard markup languageThe HyperText Markup Language or HTML is the standard markup language
The HyperText Markup Language or HTML is the standard markup language
 
Working with JSON
Working with JSONWorking with JSON
Working with JSON
 
Yargs Module
Yargs ModuleYargs Module
Yargs Module
 
NODEMON Module
NODEMON ModuleNODEMON Module
NODEMON Module
 
Getting Input from User
Getting Input from UserGetting Input from User
Getting Input from User
 
fs Module.pptx
fs Module.pptxfs Module.pptx
fs Module.pptx
 
Transaction Processing in DBMS.pptx
Transaction Processing in DBMS.pptxTransaction Processing in DBMS.pptx
Transaction Processing in DBMS.pptx
 
web_server_browser.ppt
web_server_browser.pptweb_server_browser.ppt
web_server_browser.ppt
 
Web Server.pptx
Web Server.pptxWeb Server.pptx
Web Server.pptx
 
Number System.pptx
Number System.pptxNumber System.pptx
Number System.pptx
 
Programming Language.ppt
Programming Language.pptProgramming Language.ppt
Programming Language.ppt
 
Information System.pptx
Information System.pptxInformation System.pptx
Information System.pptx
 
Applications of Computer Science in Pharmacy-1.pptx
Applications of Computer Science in Pharmacy-1.pptxApplications of Computer Science in Pharmacy-1.pptx
Applications of Computer Science in Pharmacy-1.pptx
 
Application of Computers in Pharmacy.pptx
Application of Computers in Pharmacy.pptxApplication of Computers in Pharmacy.pptx
Application of Computers in Pharmacy.pptx
 
Deploying your app.pptx
Deploying your app.pptxDeploying your app.pptx
Deploying your app.pptx
 
Setting up github and ssh keys.ppt
Setting up github and ssh keys.pptSetting up github and ssh keys.ppt
Setting up github and ssh keys.ppt
 
Adding a New Feature and Deploying.ppt
Adding a New Feature and Deploying.pptAdding a New Feature and Deploying.ppt
Adding a New Feature and Deploying.ppt
 
Requiring your own files.pptx
Requiring your own files.pptxRequiring your own files.pptx
Requiring your own files.pptx
 
Unit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptxUnit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptx
 
Yargs Module.pptx
Yargs Module.pptxYargs Module.pptx
Yargs Module.pptx
 

Recently uploaded

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesChandrakantDivate1
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 

Recently uploaded (20)

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

Exception handling in java

  • 1. EXCEPTION HANDLING IN JAVA Tips and tools for creating and presenting wide format slides
  • 2.  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.
  • 3. Types of Exception  Checked Exception  Unchecked Exception  Error
  • 4.  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.
  • 5.  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.
  • 6.  Error  Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 7.  Common scenarios where exceptions may occur
  • 8.  Scenario where ArithmeticException occurs  Scenario where NullPointerException occurs  Scenario where NumberFormatException occurs  Scenario where ArrayIndexOutOfBoundsException occurs
  • 9.  Java Exception Handling Keywords
  • 10.  Java try-catch:  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.
  • 11.  Syntax of java try-catch  Try  {  //code that may throw exception  }  catch(Exception_class_Name ref)  {}
  • 12.  Syntax of try-finally block:  Try  {  //code that may throw exception  }  Finally  {}
  • 13.  Java catch block:  Java catch block is used to handle the Exception. It must be used after the try block only.  You can use multiple catch block with a single try.
  • 14.  Problem without exception handling
  • 15.  public class Testtrycatch1  {  public static void main(String args[])  {  int data=50/0;//may throw exception  System.out.println("rest of the code...");  }  }
  • 16.  Solution by exception handling
  • 17.  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...");  } }
  • 18.  Internal working of java try-catch block
  • 19.
  • 20.  Java catch multiple exceptions
  • 21.  public class TestMultipleCatchBlock{  public static void main(String args[]){  try  {  int a[]=new int[5];  a[5]=30/0;  }  catch(ArithmeticException e){System.out.println("task1 is complet ed");}  catch(ArrayIndexOutOfBoundsException e){System.out.println("ta sk 2 completed");}  catch(Exception e){System.out.println("common task completed") ;}   System.out.println("rest of the code...");  } }
  • 22.  Rule: At a time only one Exception is occured and at a time only one catch block is executed.  Rule: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception .
  • 23.  class TestMultipleCatchBlock1{  public static void main(String args[]){  try  {  int a[]=new int[5];  a[15]=30;  }  catch(Exception e){System.out.println("common task completed ");}  catch(ArithmeticException e){System.out.println("task1 is compl eted");}  catch(ArrayIndexOutOfBoundsException e){System.out.println(" task 2 completed");}  System.out.println("rest of the code...");
  • 24.  Java Nested try block  The try block within a try block is known as nested try block in java.
  • 25.  Why use nested try block  Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.
  • 26.  try  {  statement 1;  try  {  statement 1;  }  catch(Exception e)  {  }  }  catch(Exception e)  {  }
  • 27.  Java finally block:  Java finally block is a block that is used to execute important code such as closing connection, stream etc.  Java finally block is always executed whether exception is handled or not.  Java finally block follows try or catch block.
  • 28.
  • 29.  Note: If you don't handle exception, before terminating the program, JVM executes finally block(if any).
  • 30.  Why use java finally  Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
  • 31. class TestFinallyBlock{ public static void main(String args[]){ try{ int data=25/5; System.out.println(data); } catch(NullPointerException e){System.out.println(e) ;} finally{System.out.println("finally block is always ex ecuted");} System.out.println("rest of the code..."); } }
  • 32.  Rule: For each try block there can be zero or more catch blocks, but only one finally block.
  • 33.  Java throw keyword:  The Java throw keyword is used to explicitly throw an exception.  We can throw either checked or uncheked exception in java by throw keyword.
  • 34.  The syntax of java throw keyword is given below.  throw exception;
  • 35.  Example of throw IOException.  throw new IOException("sorry device error);
  • 36.  Java throw keyword example
  • 37.  In this example, we have created the validate method that takes integer value as a parameter. If the age is less than 18, we are throwing the ArithmeticException otherwise print a message welcome to vote.
  • 38.  public class TestThrow1{  static void validate(int age){  if(age<18)  throw new ArithmeticException("not valid");  else  System.out.println("welcome to vote");  }  public static void main(String args[]){  validate(13);  System.out.println("rest of the code..."); } }
  • 39.  Java throws keyword
  • 40.  The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.  Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.
  • 41.  Syntax of java throws:  returnType method() throws exceptionClassNam e  {  //method code  }
  • 42.  Java throws example