SlideShare uma empresa Scribd logo
1 de 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
..………………………………………………………………………………

Mais conteúdo relacionado

Mais procurados

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 

Mais procurados (20)

Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception handling
Exception handlingException handling
Exception handling
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Exception handling
Exception handlingException handling
Exception handling
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core 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
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Exception Handling
Exception HandlingException Handling
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
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 

Semelhante a Exception handling in java

Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
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
 
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
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
Ravindra Rathore
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
Kiran Munir
 

Semelhante a 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
 
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
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

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