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
..………………………………………………………………………………
Ad

More Related Content

What's hot (20)

L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Exception handling
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
Ganesh kumar reddy
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
Sunil Kumar Gunasekaran
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Abhishek Pachisia
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Operators in java
Operators in javaOperators in java
Operators in java
Madishetty Prathibha
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Lovely Professional University
 

Similar to Exception handling in java (20)

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
 
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMINGUNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
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
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
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 - ii
Ravindra Rathore
 
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
 
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMINGUNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
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
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
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
 
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 - ii
Ravindra Rathore
 
Ad

More from Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET)) (6)

Basic Relationships between Pixels- Digital Image Processing
Basic Relationships between Pixels- Digital Image ProcessingBasic Relationships between Pixels- Digital Image Processing
Basic Relationships between Pixels- Digital Image Processing
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
IP address and IP class - Computer Networking
IP address and IP class - Computer NetworkingIP address and IP class - Computer Networking
IP address and IP class - Computer Networking
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
Introduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer NetworkingIntroduction to IP address concept - Computer Networking
Introduction to IP address concept - Computer Networking
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
8251 a basic
8251 a basic8251 a basic
8251 a basic
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
Computer networking
Computer networkingComputer networking
Computer networking
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
What is design pattern
What is design patternWhat is design pattern
What is design pattern
Md.Shohel Rana ( M.Sc in CSE Khulna University of Engineering & Technology (KUET))
 
Ad

Recently uploaded (20)

DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
Imma Valls Bernaus
 
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
saimabibi60507
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
🌱 Green Grafana 🌱 Essentials_ Data, Visualizations and Plugins.pdf
Imma Valls Bernaus
 
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
PRTG Network Monitor Crack Latest Version & Serial Key 2025 [100% Working]
saimabibi60507
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 

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