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

Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
Md. Tanvir Hossain
 
Exception handling
Exception handling Exception handling
Exception handling
M Vishnuvardhan Reddy
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
QaziUmarF786
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 

What's hot (20)

Exception handling
Exception handlingException handling
Exception handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception handling
Exception handling Exception handling
Exception handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java exception
Java exception Java exception
Java exception
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
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 in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java I/O
Java I/OJava I/O
Java I/O
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 

Similar to Exception handling in java

Java exception handling
Java exception handlingJava exception handling
Java exception handling
GaneshKumarKanthiah
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
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
rohitgudasi18
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
Exceptions
ExceptionsExceptions
Exceptions
motthu18
 
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
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
MaddalaSeshu
 
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 java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
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
 
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
 

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 Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
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
 
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
 
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 .
 

Recently uploaded

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

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"); } } }