SlideShare a Scribd company logo
1 of 12
Exception Handling In
Java
By Priyanka zope
Content
1:Introduction.
2:Error and Error Handling.
3:Exception.
4:Type Of Exception.
5:Coding Exception.
6:Advantages
Error and Error Handling.
v
An Error is any unexpected result obtained from a program during execution.
v
Unhandled errors may manifest themselves as incorrect
results or behavior, or as abnormal program termination.
v
Errors should be handled by the programmer, to prevent
them from reaching the user.
Exception
Exception- a better error handling.
Exception – an error condition that can occur during the
course of a program
execution
Ø
In Java, exceptions are objects themselves
Exception handling is another form of control structure
(like ifs and switch statements)
Ø
When an error is encountered, the normal flow
of the program is stopped and the exception is
Handled.
Types Of Exception
I:Checked Exception
Checked at compile time.
II:Unchecked Exception
Checked at runtime.
Coding Exception
1: try…..catch block
2:finally
3: throw
Try…catch block
•
To process an exception when it occurs, the line that throws the exception is
executed within a try block.
• A try block is followed by one or more catch clauses, which
contain code to process an exception.
•
The catch statement is used for catching exceptions.
try
{
<try block>
}
catch ( <ExceptionClass> <name> )
{
<catch block>
}
catch ( <ExceptionClass> <name> )
{
<catch block>}
Finally clause
•
A try statement can have an optional clause designated by
the reserved word finally.
• If no exception is generated, the statements in the finally
clause are executed after the statements in the try block
complete.
try {
// statements that throw exceptions
} catch(<exception>) {
// do stuff
} finally {
– // code here runs whether or not catch runs
}
Throw
All methods use the throw statement to throw an exception.
The throw statement requires a single argument: a throwable object.
Throwable objects are instances of any subclass of the Throwable class. Here's
an example of a throw statement.
Problem
IllegalArgumentException: illegal
parameter value
Problem
public class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
{
?????????
}
balance = balance - amount;
}
. . .}
Solution
public class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
{
IllegalArgumentException exception
= new IllegalArgumentException("Amount
exceeds balance");
throw exception;
}
balance = balance - amount;
}
. . .
}
Advantages
1:Separating Error-Handling Code from "Regular" Code
2:Grouping and Differentiating Error Types
3:consistency
4:flexibility
5:simplicity

More Related Content

What's hot

Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
aptechsravan
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
Deepak Sharma
 

What's hot (20)

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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 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
 
exception handling
exception handlingexception handling
exception handling
 
Java exception
Java exception Java exception
Java exception
 
javaexceptions
javaexceptionsjavaexceptions
javaexceptions
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 

Similar to Exception handling in java

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 Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Java class 7
Java class 7Java class 7
Java class 7
Edureka!
 

Similar to Exception handling in java (20)

Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
JAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdfJAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdf
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
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 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
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
 
Java class 7
Java class 7Java class 7
Java class 7
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Exception
ExceptionException
Exception
 
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & MultithreadingB.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Exception handling in java

  • 2. Content 1:Introduction. 2:Error and Error Handling. 3:Exception. 4:Type Of Exception. 5:Coding Exception. 6:Advantages
  • 3. Error and Error Handling. v An Error is any unexpected result obtained from a program during execution. v Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. v Errors should be handled by the programmer, to prevent them from reaching the user.
  • 4. Exception Exception- a better error handling. Exception – an error condition that can occur during the course of a program execution Ø In Java, exceptions are objects themselves Exception handling is another form of control structure (like ifs and switch statements) Ø When an error is encountered, the normal flow of the program is stopped and the exception is Handled.
  • 5. Types Of Exception I:Checked Exception Checked at compile time. II:Unchecked Exception Checked at runtime.
  • 6. Coding Exception 1: try…..catch block 2:finally 3: throw
  • 7. Try…catch block • To process an exception when it occurs, the line that throws the exception is executed within a try block. • A try block is followed by one or more catch clauses, which contain code to process an exception. • The catch statement is used for catching exceptions. try { <try block> } catch ( <ExceptionClass> <name> ) { <catch block> } catch ( <ExceptionClass> <name> ) { <catch block>}
  • 8. Finally clause • A try statement can have an optional clause designated by the reserved word finally. • If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete. try { // statements that throw exceptions } catch(<exception>) { // do stuff } finally { – // code here runs whether or not catch runs }
  • 9. Throw All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.
  • 10. Problem IllegalArgumentException: illegal parameter value Problem public class BankAccount { public void withdraw(double amount) { if (amount > balance) { ????????? } balance = balance - amount; } . . .}
  • 11. Solution public class BankAccount { public void withdraw(double amount) { if (amount > balance) { IllegalArgumentException exception = new IllegalArgumentException("Amount exceeds balance"); throw exception; } balance = balance - amount; } . . . }
  • 12. Advantages 1:Separating Error-Handling Code from "Regular" Code 2:Grouping and Differentiating Error Types 3:consistency 4:flexibility 5:simplicity