SlideShare a Scribd company logo
1 of 19
Topics : Exception handling in java
created by……
Md .shohel Rana
Roll:140135
2nd year 2nd semester
Dept. of Cse
Exception Handling
An exception is an abnormal condition in which the
normal execution of code gets hampered at run
time.
The error handling mechanism that is called
exception handling.
When occur Exception in a program
Some of the common runtimes errors
1) Dividing a number by zero
2) Accessing an element that is out of bounds of an array
3) Using negative value as array size
4) Converting string to integer
5) File error
6) Corrupting memory
Why we use exception handling macanisum
 Notify the error
 Save all work
 Allow graceful termination of a program
try, catch, finally keywords
the code which might throw some exceptions should be
kept in try block.
the catch block can hava the code to handle the
exception
finally block can be used to clean up code or release
some resources that are utilized in the program
using try,catch and finally keywords
There are three forms of try statement:
1) try-catch
A try block followed by one or more catch blocks.
2) try-finally
A try block followed by a finally blocks.
3) try-catch-finally
A try blocks followed by one or more catch blocks followed by a finally block.
using try,catch and finally keywords
 Syntax:
Try{
//code that might cause an exception is kept here
}catch(ExceptionType obj){
//when an exception occurs, the control comes here
} catch(ExceptionType obj){
//when an exception occurs, the control comes here
}
using try,catch and finally keywords
 Syntax:
Try{
//code that might cause an exception is kept here
}finally{
//release some resources here
}
using try,catch and finally keywords
 Syntax:
Try{
//code that might cause an exception is kept here
}catch(ExceptionType obj){
//when an exception occurs, the control comes here
}finally{
//release some resources here
}
try,catch and finally-Rules
 When there is a try block, catch or finally block should be
there
 There should not be any statements between try and catch
and finally
 Finally block is optional.
 Only one finally block for a try block.
 There can be multiple catch blocks for a try block.
Types of Exception
Exception
Built-in
Exception
User-defined
Exception
Some java Pre-defined /Built-in exception class
Java.lang.Exception is a super class for all pre-defined exception class.
1. Exception ---------------------------------------------------> Base class for all exception objects.
2. SystemException------------------------------------------> Base class for all runtime errors.
3. ArithmeticException-------------------------------------> Arithmetic error.
4. ArrayIndexOutOfBoundsException-----------------> array index out of bounds
5. NegativeArraySizeException--------------------------> Array created with a negative size.
6. NullPointerException------------------------------------->invalid use of a null reference.
7. FileNotFoundException---------------------------------> when file is not found
8. ClassNotFoundException----------------------------- >when class is not found
9. NumberFormatException------------------------------> invalid conversion of a string ot a number format
NumberFormatException
1) -----correct
String str1="10";
System.out.println("this is string :"+str1);
int x=Integer.parseInt(str1);
System.out.println("after convart string to int: "+x*x);
2)-----exception
String str2="ten";
int y=Integer.parseInt(str2);
System.out.println("this is string :"+str2);
IndexOutOfBoundsException
-------there are two types………..
1. ArrayIndexOutOfBoundsException
2. StringIndexOutOfBoundsException
……………………………………………..
int [] marks={3,4,5};
System.out.println(marks[4]); //Wrong index number
………………………………………………
String str="hello";
System.out.println(str.charAt(5)); //Wrong character index number
FileNotFoundException
try {
File inputfile=new File("c:file.txt");
FileReader f=new FileReader(inputfile);
}
catch (FileNotFoundException e) {
System.out.println("file not found");
}
User-defined Exceptions
 Most of the times when we are developing an application in java, we
often feel a need to create and throw our own exceptions.These
exceptions are known as user-defined or custom exceptions.
Point ot remember:
1. user-defined exception needs to inherit (extends) Exception class in
order to act as an exception.
2. throw keyword is used to throw such exceptions.
Using super() keyword
Part-1:
class DemoException extends Exception
{
public DemoException(String p)
{
super("i got an exception "+p); //call the main Exception class
}
}
Part-2:
public class userDef {
public static void main(String args[])
{
int balance = 100, withdraw = 1000;
if (balance < withdraw) {
try{
DemoException e = new DemoException("money is short");
throw e; //this throw the main exception class not DemoException class
}
catch(DemoException e) {
System.out.println("insufficient balancenn" + e);
}
}
else {
System.out.println("Draw & enjoy Sir, Best wishes of the day");
} }
}
The End
..………………………………………………………………………………

More Related Content

What's hot

Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java AppletsTareq Hasan
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in JavaAnkit Rai
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptionsmyrajendra
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaARAFAT ISLAM
 

What's hot (20)

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
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Java exception
Java exception Java exception
Java exception
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
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
Java Exception handlingJava Exception handling
Java Exception handling
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 

Similar to Exception handling in java

Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxNagaraju Pamarthi
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javagopalrajput11
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Exceptions
ExceptionsExceptions
Exceptionsmotthu18
 
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
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdfMaddalaSeshu
 
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
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - iiRavindra Rathore
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .happycocoman
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRKiran Munir
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 

Similar to Exception handling in java (20)

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
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exceptions
ExceptionsExceptions
Exceptions
 
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
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
 
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 java Exception handling in java
Exception handling in java
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 

Recently uploaded

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

Exception handling in java

  • 1. Topics : Exception handling in java created by…… Md .shohel Rana Roll:140135 2nd year 2nd semester Dept. of Cse
  • 2. Exception Handling An exception is an abnormal condition in which the normal execution of code gets hampered at run time. The error handling mechanism that is called exception handling.
  • 3. When occur Exception in a program Some of the common runtimes errors 1) Dividing a number by zero 2) Accessing an element that is out of bounds of an array 3) Using negative value as array size 4) Converting string to integer 5) File error 6) Corrupting memory
  • 4. Why we use exception handling macanisum  Notify the error  Save all work  Allow graceful termination of a program
  • 5. try, catch, finally keywords the code which might throw some exceptions should be kept in try block. the catch block can hava the code to handle the exception finally block can be used to clean up code or release some resources that are utilized in the program
  • 6. using try,catch and finally keywords There are three forms of try statement: 1) try-catch A try block followed by one or more catch blocks. 2) try-finally A try block followed by a finally blocks. 3) try-catch-finally A try blocks followed by one or more catch blocks followed by a finally block.
  • 7. using try,catch and finally keywords  Syntax: Try{ //code that might cause an exception is kept here }catch(ExceptionType obj){ //when an exception occurs, the control comes here } catch(ExceptionType obj){ //when an exception occurs, the control comes here }
  • 8. using try,catch and finally keywords  Syntax: Try{ //code that might cause an exception is kept here }finally{ //release some resources here }
  • 9. using try,catch and finally keywords  Syntax: Try{ //code that might cause an exception is kept here }catch(ExceptionType obj){ //when an exception occurs, the control comes here }finally{ //release some resources here }
  • 10. try,catch and finally-Rules  When there is a try block, catch or finally block should be there  There should not be any statements between try and catch and finally  Finally block is optional.  Only one finally block for a try block.  There can be multiple catch blocks for a try block.
  • 12. Some java Pre-defined /Built-in exception class Java.lang.Exception is a super class for all pre-defined exception class. 1. Exception ---------------------------------------------------> Base class for all exception objects. 2. SystemException------------------------------------------> Base class for all runtime errors. 3. ArithmeticException-------------------------------------> Arithmetic error. 4. ArrayIndexOutOfBoundsException-----------------> array index out of bounds 5. NegativeArraySizeException--------------------------> Array created with a negative size. 6. NullPointerException------------------------------------->invalid use of a null reference. 7. FileNotFoundException---------------------------------> when file is not found 8. ClassNotFoundException----------------------------- >when class is not found 9. NumberFormatException------------------------------> invalid conversion of a string ot a number format
  • 13. NumberFormatException 1) -----correct String str1="10"; System.out.println("this is string :"+str1); int x=Integer.parseInt(str1); System.out.println("after convart string to int: "+x*x); 2)-----exception String str2="ten"; int y=Integer.parseInt(str2); System.out.println("this is string :"+str2);
  • 14. IndexOutOfBoundsException -------there are two types……….. 1. ArrayIndexOutOfBoundsException 2. StringIndexOutOfBoundsException …………………………………………….. int [] marks={3,4,5}; System.out.println(marks[4]); //Wrong index number ……………………………………………… String str="hello"; System.out.println(str.charAt(5)); //Wrong character index number
  • 15. FileNotFoundException try { File inputfile=new File("c:file.txt"); FileReader f=new FileReader(inputfile); } catch (FileNotFoundException e) { System.out.println("file not found"); }
  • 16. User-defined Exceptions  Most of the times when we are developing an application in java, we often feel a need to create and throw our own exceptions.These exceptions are known as user-defined or custom exceptions. Point ot remember: 1. user-defined exception needs to inherit (extends) Exception class in order to act as an exception. 2. throw keyword is used to throw such exceptions.
  • 17. Using super() keyword Part-1: class DemoException extends Exception { public DemoException(String p) { super("i got an exception "+p); //call the main Exception class } }
  • 18. Part-2: public class userDef { public static void main(String args[]) { int balance = 100, withdraw = 1000; if (balance < withdraw) { try{ DemoException e = new DemoException("money is short"); throw e; //this throw the main exception class not DemoException class } catch(DemoException e) { System.out.println("insufficient balancenn" + e); } } else { System.out.println("Draw & enjoy Sir, Best wishes of the day"); } } }