SlideShare a Scribd company logo
Exception Handling in Java
Explained By:
Sarbjit Kaur.
Lecturer, Department of Computer Application,
PGG.C.G., Sector: 42, Chandigarh
Objectives
• Introduction
• What exceptions are for
• Catching & Throwing exceptions
• Exception Specifications
• Standard Java Exceptions
• Exceptions and Polymorphism
• The finally clause
• Resource Management
• Uncaught Exceptions
Introduction
What is Exception ?
Due to design errors or coding errors, our programs
may fail in unexpected ways during execution.
An exception is a condition that is caused by run
time error in the program.
The purpose of the exception handling mechanism
is to provide a means to detect and report an
“ecxceptional circumstances” .
Error
• An error may produce an incorrect output or
may terminate the execution of the program
abruptly or even may cause the system to
crash. So it is our responsibility to detect and
manage the error properly.
Types of error
• Runtime Errors: occur while the program is
running if the environment detects an
operation that is impossible to carry out.
• Logic Errors: occur when a program doesn't
perform the way it was intended
• Syntax Errors: Arise because the rules of the
language have not been followed. They are
detected by the compiler.
Example of Run Time error
Class Error
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x=a/(b+c);
System.out.println("x=" +x);
int y=a/(b-c); // Errorr division by zero
System.out.println("y=" +y);
}
}
Errors and Error Handling
• Some typical causes of errors:
– Memory errors (i.e. memory incorrectly allocated,
memory leaks, “null pointer”)
– File system errors (i.e. disk is full, disk has been
removed)
– Network errors (i.e. network is down, URL does
not exist)
– Calculation errors (i.e. divide by 0)
Errors and Error Handling
• More typical causes of errors:
– Array errors (i.e. accessing element –1)
– Conversion errors (i.e. convert ‘q’ to a number)
– Can you think of some others?
Errors and Error Handling
• Exceptions – a better error handling
– Exceptions act similar to method return flags in
that any method may raise and exception should
it encounter an error.
– exceptions are handled at many levels in a
program, locally and/or globally.
Exceptions
• How do you handle exceptions?
– To handle the exception, you write a “try-catch”
block. To pass the exception “up the chain”, you
declare a throws clause in your method or class
declaration.
– If the method contains code that may cause a
checked exception, you MUST handle the
exception OR pass the exception to the parent
class (remember, every class has Object as the
ultimate parent)
Coding Exceptions
• Coding Exceptions
• Try-Catch Mechanism
– Wherever your code may trigger an exception, the
normal code logic is placed inside a block of code
starting with the “try” keyword:
– After the try block, the code to handle the
exception should it arise is placed in a block of
code starting with the “catch” keyword.
Standard Java Exceptions
Throwable
Exception Error
Runtime
Exception
IO Exception
Catching Exceptions
• Wrap code to be checked in a try-block
– checking occurs all the way down the execution
stack
• try-blocks can be nested
– control resumes at most enclosed matching
handler
Coding Exceptions
• Example
– try {
… normal program code
}
catch(Exception e) {
… exception handling code
}
Coding Exceptions
• Types of Exceptions
– Examples:
• public void myMethod throws Exception {
• public void myMethod throws IOException {
• try { … }
catch (Exception e) { … }
• try { … }
catch (IOException ioe) { … }
Code Examples
• 1. Demonstration of an unchecked exception
(NullPointerException)
• 2. Demonstration of checked exceptions:
– Passing a DivideByZeroException
– Handling a DivideByZeroException
Example
class error2
{
public static void main(String arg[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println(“Division by Zero”);
}
Y=a/(b-c);
System.out.println(“y=“+y);
}
}
In the previous program we cannot see the
value of x just because of the error in the
value of y, that is division by zero but when
we use the try and catch blocks in exception
handling then we can see the value of y which
is correct and our program will display an
error message shown in the try block.
conclusion
– Exceptions are a powerful error handling
mechanism.
– Exceptions in Java are built into the language.
– Exceptions can be handled by the programmer
(try-catch), or handled by the Java environment
(throws).
– Exception handling can only hide the errors.
– It cannot correct the errors.

More Related Content

Similar to exception-handling-in-java programming.ppt (20)

Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
MaqdamYasir
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
excenhlklklknjlnjknkjlnkjnkjnkptions.pptexcenhlklklknjlnjknkjlnkjnkjnkptions.ppt
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
kushal797779
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Exception handling
Exception handlingException handling
Exception handling
Ardhendu Nandi
 
Exception handling
Exception handlingException handling
Exception handling
Muthukumaran Subramanian
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
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
 
using Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptxusing Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
SanthiNivas
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Exception Handling in java masters of computer application
Exception Handling in java masters of computer applicationException Handling in java masters of computer application
Exception Handling in java masters of computer application
xidileh999
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
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
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdf
Furkan Furkan
 
Exception handling
Exception handlingException handling
Exception handling
Karthik Sekar
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
Tareq Hasan
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
MaqdamYasir
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
excenhlklklknjlnjknkjlnkjnkjnkptions.pptexcenhlklklknjlnjknkjlnkjnkjnkptions.ppt
excenhlklklknjlnjknkjlnkjnkjnkptions.ppt
kushal797779
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
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
 
using Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptxusing Java Exception Handling in Java.pptx
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
FacultyAnupamaAlagan
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
SanthiNivas
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Exception Handling in java masters of computer application
Exception Handling in java masters of computer applicationException Handling in java masters of computer application
Exception Handling in java masters of computer application
xidileh999
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.ppt
promila09
 
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
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdf
Furkan Furkan
 

More from ansariparveen06 (20)

Preprocessing of data mining process.ppt
Preprocessing of data mining process.pptPreprocessing of data mining process.ppt
Preprocessing of data mining process.ppt
ansariparveen06
 
8. Ozone and Environmental issue and solution.ppt
8. Ozone and Environmental issue and solution.ppt8. Ozone and Environmental issue and solution.ppt
8. Ozone and Environmental issue and solution.ppt
ansariparveen06
 
ALP intro assembly language programing.pptx
ALP intro assembly language programing.pptxALP intro assembly language programing.pptx
ALP intro assembly language programing.pptx
ansariparveen06
 
cpphtp9_Exception handling in c++ .ppt
cpphtp9_Exception handling in c++   .pptcpphtp9_Exception handling in c++   .ppt
cpphtp9_Exception handling in c++ .ppt
ansariparveen06
 
introduction to javascript concepts .ppt
introduction to javascript concepts .pptintroduction to javascript concepts .ppt
introduction to javascript concepts .ppt
ansariparveen06
 
java multi threading and synchronisation.ppt
java multi threading and synchronisation.pptjava multi threading and synchronisation.ppt
java multi threading and synchronisation.ppt
ansariparveen06
 
Environmental studies part2 bscit sem2.pptx
Environmental studies part2 bscit sem2.pptxEnvironmental studies part2 bscit sem2.pptx
Environmental studies part2 bscit sem2.pptx
ansariparveen06
 
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptxENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ansariparveen06
 
brief introduction to core java programming.pptx
brief introduction to core java programming.pptxbrief introduction to core java programming.pptx
brief introduction to core java programming.pptx
ansariparveen06
 
Module1 evs Environmental Pollution.pptx
Module1 evs Environmental Pollution.pptxModule1 evs Environmental Pollution.pptx
Module1 evs Environmental Pollution.pptx
ansariparveen06
 
Internet_Banking e commerce in banking.pptx
Internet_Banking e commerce in banking.pptxInternet_Banking e commerce in banking.pptx
Internet_Banking e commerce in banking.pptx
ansariparveen06
 
UNIT1 Decision Support System in BI.pptx
UNIT1 Decision Support System in BI.pptxUNIT1 Decision Support System in BI.pptx
UNIT1 Decision Support System in BI.pptx
ansariparveen06
 
logic gate based on discrete mathematics.ppt
logic gate based on discrete mathematics.pptlogic gate based on discrete mathematics.ppt
logic gate based on discrete mathematics.ppt
ansariparveen06
 
Overview on how to Disciplining in life .pptx
Overview on how to Disciplining in life .pptxOverview on how to Disciplining in life .pptx
Overview on how to Disciplining in life .pptx
ansariparveen06
 
presentation on java server pages vs servlet.ppt
presentation on java server pages vs servlet.pptpresentation on java server pages vs servlet.ppt
presentation on java server pages vs servlet.ppt
ansariparveen06
 
Introduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).pptIntroduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
enterprise java - introduction to servlet.pptx
enterprise java - introduction to servlet.pptxenterprise java - introduction to servlet.pptx
enterprise java - introduction to servlet.pptx
ansariparveen06
 
Introduction to Operating - Systems.pptx
Introduction to Operating - Systems.pptxIntroduction to Operating - Systems.pptx
Introduction to Operating - Systems.pptx
ansariparveen06
 
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdfAdvanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
ansariparveen06
 
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdfAdvanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
ansariparveen06
 
Preprocessing of data mining process.ppt
Preprocessing of data mining process.pptPreprocessing of data mining process.ppt
Preprocessing of data mining process.ppt
ansariparveen06
 
8. Ozone and Environmental issue and solution.ppt
8. Ozone and Environmental issue and solution.ppt8. Ozone and Environmental issue and solution.ppt
8. Ozone and Environmental issue and solution.ppt
ansariparveen06
 
ALP intro assembly language programing.pptx
ALP intro assembly language programing.pptxALP intro assembly language programing.pptx
ALP intro assembly language programing.pptx
ansariparveen06
 
cpphtp9_Exception handling in c++ .ppt
cpphtp9_Exception handling in c++   .pptcpphtp9_Exception handling in c++   .ppt
cpphtp9_Exception handling in c++ .ppt
ansariparveen06
 
introduction to javascript concepts .ppt
introduction to javascript concepts .pptintroduction to javascript concepts .ppt
introduction to javascript concepts .ppt
ansariparveen06
 
java multi threading and synchronisation.ppt
java multi threading and synchronisation.pptjava multi threading and synchronisation.ppt
java multi threading and synchronisation.ppt
ansariparveen06
 
Environmental studies part2 bscit sem2.pptx
Environmental studies part2 bscit sem2.pptxEnvironmental studies part2 bscit sem2.pptx
Environmental studies part2 bscit sem2.pptx
ansariparveen06
 
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptxENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ENVIRONMENTAL STUDIES FYBSCIT SEM 2.pptx
ansariparveen06
 
brief introduction to core java programming.pptx
brief introduction to core java programming.pptxbrief introduction to core java programming.pptx
brief introduction to core java programming.pptx
ansariparveen06
 
Module1 evs Environmental Pollution.pptx
Module1 evs Environmental Pollution.pptxModule1 evs Environmental Pollution.pptx
Module1 evs Environmental Pollution.pptx
ansariparveen06
 
Internet_Banking e commerce in banking.pptx
Internet_Banking e commerce in banking.pptxInternet_Banking e commerce in banking.pptx
Internet_Banking e commerce in banking.pptx
ansariparveen06
 
UNIT1 Decision Support System in BI.pptx
UNIT1 Decision Support System in BI.pptxUNIT1 Decision Support System in BI.pptx
UNIT1 Decision Support System in BI.pptx
ansariparveen06
 
logic gate based on discrete mathematics.ppt
logic gate based on discrete mathematics.pptlogic gate based on discrete mathematics.ppt
logic gate based on discrete mathematics.ppt
ansariparveen06
 
Overview on how to Disciplining in life .pptx
Overview on how to Disciplining in life .pptxOverview on how to Disciplining in life .pptx
Overview on how to Disciplining in life .pptx
ansariparveen06
 
presentation on java server pages vs servlet.ppt
presentation on java server pages vs servlet.pptpresentation on java server pages vs servlet.ppt
presentation on java server pages vs servlet.ppt
ansariparveen06
 
Introduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).pptIntroduction to Java Servlets and JSP (1).ppt
Introduction to Java Servlets and JSP (1).ppt
ansariparveen06
 
enterprise java - introduction to servlet.pptx
enterprise java - introduction to servlet.pptxenterprise java - introduction to servlet.pptx
enterprise java - introduction to servlet.pptx
ansariparveen06
 
Introduction to Operating - Systems.pptx
Introduction to Operating - Systems.pptxIntroduction to Operating - Systems.pptx
Introduction to Operating - Systems.pptx
ansariparveen06
 
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdfAdvanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
ansariparveen06
 
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdfAdvanced Web Programming_UNIT_1_NewSyllabus.pdf
Advanced Web Programming_UNIT_1_NewSyllabus.pdf
ansariparveen06
 
Ad

Recently uploaded (20)

Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
Ad

exception-handling-in-java programming.ppt

  • 1. Exception Handling in Java Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh
  • 2. Objectives • Introduction • What exceptions are for • Catching & Throwing exceptions • Exception Specifications • Standard Java Exceptions • Exceptions and Polymorphism • The finally clause • Resource Management • Uncaught Exceptions
  • 3. Introduction What is Exception ? Due to design errors or coding errors, our programs may fail in unexpected ways during execution. An exception is a condition that is caused by run time error in the program. The purpose of the exception handling mechanism is to provide a means to detect and report an “ecxceptional circumstances” .
  • 4. Error • An error may produce an incorrect output or may terminate the execution of the program abruptly or even may cause the system to crash. So it is our responsibility to detect and manage the error properly.
  • 5. Types of error • Runtime Errors: occur while the program is running if the environment detects an operation that is impossible to carry out. • Logic Errors: occur when a program doesn't perform the way it was intended • Syntax Errors: Arise because the rules of the language have not been followed. They are detected by the compiler.
  • 6. Example of Run Time error Class Error { public static void main(String args[]) { int a=10; int b=5; int c=5; int x=a/(b+c); System.out.println("x=" +x); int y=a/(b-c); // Errorr division by zero System.out.println("y=" +y); } }
  • 7. Errors and Error Handling • Some typical causes of errors: – Memory errors (i.e. memory incorrectly allocated, memory leaks, “null pointer”) – File system errors (i.e. disk is full, disk has been removed) – Network errors (i.e. network is down, URL does not exist) – Calculation errors (i.e. divide by 0)
  • 8. Errors and Error Handling • More typical causes of errors: – Array errors (i.e. accessing element –1) – Conversion errors (i.e. convert ‘q’ to a number) – Can you think of some others?
  • 9. Errors and Error Handling • Exceptions – a better error handling – Exceptions act similar to method return flags in that any method may raise and exception should it encounter an error. – exceptions are handled at many levels in a program, locally and/or globally.
  • 10. Exceptions • How do you handle exceptions? – To handle the exception, you write a “try-catch” block. To pass the exception “up the chain”, you declare a throws clause in your method or class declaration. – If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception to the parent class (remember, every class has Object as the ultimate parent)
  • 11. Coding Exceptions • Coding Exceptions • Try-Catch Mechanism – Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: – After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword.
  • 12. Standard Java Exceptions Throwable Exception Error Runtime Exception IO Exception
  • 13. Catching Exceptions • Wrap code to be checked in a try-block – checking occurs all the way down the execution stack • try-blocks can be nested – control resumes at most enclosed matching handler
  • 14. Coding Exceptions • Example – try { … normal program code } catch(Exception e) { … exception handling code }
  • 15. Coding Exceptions • Types of Exceptions – Examples: • public void myMethod throws Exception { • public void myMethod throws IOException { • try { … } catch (Exception e) { … } • try { … } catch (IOException ioe) { … }
  • 16. Code Examples • 1. Demonstration of an unchecked exception (NullPointerException) • 2. Demonstration of checked exceptions: – Passing a DivideByZeroException – Handling a DivideByZeroException
  • 17. Example class error2 { public static void main(String arg[]) { int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System.out.println(“Division by Zero”); } Y=a/(b-c); System.out.println(“y=“+y); } }
  • 18. In the previous program we cannot see the value of x just because of the error in the value of y, that is division by zero but when we use the try and catch blocks in exception handling then we can see the value of y which is correct and our program will display an error message shown in the try block.
  • 19. conclusion – Exceptions are a powerful error handling mechanism. – Exceptions in Java are built into the language. – Exceptions can be handled by the programmer (try-catch), or handled by the Java environment (throws). – Exception handling can only hide the errors. – It cannot correct the errors.