SlideShare a Scribd company logo
Exception handling
• Introduction of Exception
• Define exceptions,
• Use of try, catch, and finally statements.
• Exception categories.
• Common exceptions,
• Defining own exceptions
• A Java exception is an object that describes an
exceptional (that is, error) condition that has occurred
in a piece of code.(at run time)
• When an exceptional condition arises, an object
representing that exception is created and thrown in
the method that caused the error.
• That method may choose to handle the exception
itself, or pass it on. Either way, at some point, the
exception is caught and processed.
• Exceptions can be generated by the Java run-time
system, or they can be manually generated by your
code.
• Manually generated exceptions are typically used to
report some error condition to the caller of a method.
• Java exception handling is managed via five
keywords:
• try,
• catch
• throw,
• throws,
• finally.
• Program statements that we want to monitor for
exceptions are contained within a try block.
• If an exception occurs within the try block, it is thrown.
Your code can catch this exception (using catch) and
handle it in some rational manner.
• System-generated exceptions are automatically thrown
by the Java run-time system. To manually throw an
exception, we use the keyword throw.
• Any exception that is thrown out of a method must be
specified as such by a throws clause. Any code that
absolutely must be executed before a method returns
is put in a finally block.
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed before try block ends
}
Here, ExceptionType is the type of exception that has occurred
• All exception types are subclasses of the built-in class
Throwable. Throwable is at the top of the exception
class hierarchy. Immediately below Throwable are two
subclasses that partition exceptions into two distinct
branches.
• One branch is headed by Exception.
– This class is used for exceptional conditions that user
programs should catch. This is also the class that you will
subclass to create your own custom exception types.
– There is an important subclass of Exception, called
RuntimeException. Exceptions of this type are
automatically defined for the programs that you write and
include things such as division by zero and invalid array
indexing.
Exception Types
• The other branch is topped by Error, which
defines exceptions that are not expected to be
caught under normal circumstances by our
program.
– Exceptions of type Error are used by the Java run-
time system to indicate errors having to do with
the run-time environment, itself. Stack overflow is
an example of such an error.
Throwable
Exception Error
RuntimeException
Uncaught Exceptions
class Exc0 {
public static void main(String args[]) {
int d = 0;
int a = 42 / d;
} }
• When the Java run-time system detects the attempt to
divide by zero, it constructs a new exception object and
then throws this exception. This causes the execution of
Exc0 to stop, because once an exception has been thrown,
it must be caught by an exception handler and dealt with
immediately.
• In this case, we haven’t supplied any exception handlers of
our own, so the exception is caught by the default handler
provided by the Java run-time system.
• Any exception that is not caught by our program will
ultimately be processed by the default handler. The default
handler displays a string describing the exception, prints a
stack trace from the point at which the exception occurred,
and terminates the program.
• Here is the output generated when this example is
executed.
java.lang.ArithmeticException: / by zero
at Exc0.main(Exc0.java:4)
• The type of the exception thrown is a subclass of Exception
called ArithmeticException, which more specifically
describes what type of error happened. Java supplies
several built-in exception types that match the various sorts
of run-time errors that can be generated. The stack trace
will always show the sequence of method invocations that
led up to the error.
Ad

More Related Content

What's hot (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
PRN USM
 
Exception Handling In Java 15734
Exception Handling In Java 15734Exception Handling In Java 15734
Exception Handling In Java 15734
madhurendra pandey
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
Shipra Swati
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and Applets
Tanmoy Roy
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
ARAFAT ISLAM
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Java class 7
Java class 7Java class 7
Java class 7
Edureka!
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
priyankazope
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
Terry Yoast
 
Chap12
Chap12Chap12
Chap12
Terry Yoast
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
dhrubo kayal
 
exception handling
exception handlingexception handling
exception handling
Manav Dharman
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
aptechsravan
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
Java2Blog
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Exception Handling
Exception HandlingException Handling
Exception Handling
PRN USM
 
Exception Handling In Java 15734
Exception Handling In Java 15734Exception Handling In Java 15734
Exception Handling In Java 15734
madhurendra pandey
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Java Exception Handling and Applets
Java Exception Handling and AppletsJava Exception Handling and Applets
Java Exception Handling and Applets
Tanmoy Roy
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
ARAFAT ISLAM
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Java class 7
Java class 7Java class 7
Java class 7
Edureka!
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
priyankazope
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
Terry Yoast
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
aptechsravan
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
Java2Blog
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 

Viewers also liked (20)

Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
Sasha Goldshtein
 
Oops
OopsOops
Oops
Gayathri Ganesh
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
jigeno
 
Ch11 communication
Ch11  communicationCh11  communication
Ch11 communication
adrienne0901
 
28 networking
28  networking28  networking
28 networking
Ravindra Rathore
 
Introduction of reflection
Introduction of reflection Introduction of reflection
Introduction of reflection
Ravindra Rathore
 
26 io -ii file handling
26  io -ii  file handling26  io -ii  file handling
26 io -ii file handling
Ravindra Rathore
 
21 multi threading - iii
21 multi threading - iii21 multi threading - iii
21 multi threading - iii
Ravindra Rathore
 
Lecture 5 phasor notations
Lecture 5 phasor notationsLecture 5 phasor notations
Lecture 5 phasor notations
Ravindra Rathore
 
22 multi threading iv
22 multi threading iv22 multi threading iv
22 multi threading iv
Ravindra Rathore
 
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Leon Osinski
 
Vlsi
VlsiVlsi
Vlsi
Rina Ahire
 
Collections in-csharp
Collections in-csharpCollections in-csharp
Collections in-csharp
Lakshmi Mareddy
 
Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016
Asociatia SOS Infertilitatea - www.vremcopii.ro
 
A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4
Leon Osinski
 
Fiber optics101
Fiber optics101Fiber optics101
Fiber optics101
admercano101
 
Spread spectrum
Spread spectrumSpread spectrum
Spread spectrum
Rina Ahire
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
Oracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guideOracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guide
Otto Paiz
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
jigeno
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
Sasha Goldshtein
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
jigeno
 
Ch11 communication
Ch11  communicationCh11  communication
Ch11 communication
adrienne0901
 
Introduction of reflection
Introduction of reflection Introduction of reflection
Introduction of reflection
Ravindra Rathore
 
Lecture 5 phasor notations
Lecture 5 phasor notationsLecture 5 phasor notations
Lecture 5 phasor notations
Ravindra Rathore
 
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Leon Osinski
 
A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4
Leon Osinski
 
Spread spectrum
Spread spectrumSpread spectrum
Spread spectrum
Rina Ahire
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
Oracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guideOracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guide
Otto Paiz
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
jigeno
 
Ad

Similar to 16 exception handling - i (20)

Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
ARUNPRANESHS
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
raksharao
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
happycocoman
 
Lesson 8 Handling Exceptions and Errors.pdf
Lesson 8 Handling Exceptions and Errors.pdfLesson 8 Handling Exceptions and Errors.pdf
Lesson 8 Handling Exceptions and Errors.pdf
Fethulmubin
 
Unit-4 Java ppt for BCA Students Madras Univ
Unit-4 Java ppt for BCA Students Madras UnivUnit-4 Java ppt for BCA Students Madras Univ
Unit-4 Java ppt for BCA Students Madras Univ
lavanyasujat1
 
Exception
ExceptionException
Exception
Fraboni Ec
 
Exception
ExceptionException
Exception
Young Alista
 
Exception
ExceptionException
Exception
James Wong
 
Exception
ExceptionException
Exception
Luis Goldster
 
Exception
ExceptionException
Exception
Harry Potter
 
Exception
ExceptionException
Exception
Fraboni Ec
 
Exception
ExceptionException
Exception
Tony Nguyen
 
Exception
ExceptionException
Exception
Tony Nguyen
 
Exception
ExceptionException
Exception
Hoang Nguyen
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptxModule 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
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
 
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
 
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
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
ARUNPRANESHS
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
raksharao
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
happycocoman
 
Lesson 8 Handling Exceptions and Errors.pdf
Lesson 8 Handling Exceptions and Errors.pdfLesson 8 Handling Exceptions and Errors.pdf
Lesson 8 Handling Exceptions and Errors.pdf
Fethulmubin
 
Unit-4 Java ppt for BCA Students Madras Univ
Unit-4 Java ppt for BCA Students Madras UnivUnit-4 Java ppt for BCA Students Madras Univ
Unit-4 Java ppt for BCA Students Madras Univ
lavanyasujat1
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptxModule 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
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
 
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
 
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
 
Ad

More from Ravindra Rathore (6)

Line coding
Line coding Line coding
Line coding
Ravindra Rathore
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
Ravindra Rathore
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
Ravindra Rathore
 
15 bitwise operators
15 bitwise operators15 bitwise operators
15 bitwise operators
Ravindra Rathore
 
14 interface
14  interface14  interface
14 interface
Ravindra Rathore
 
Flipflop
FlipflopFlipflop
Flipflop
Ravindra Rathore
 

Recently uploaded (20)

Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 

16 exception handling - i

  • 1. Exception handling • Introduction of Exception • Define exceptions, • Use of try, catch, and finally statements. • Exception categories. • Common exceptions, • Defining own exceptions
  • 2. • A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.(at run time) • When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. • That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed. • Exceptions can be generated by the Java run-time system, or they can be manually generated by your code. • Manually generated exceptions are typically used to report some error condition to the caller of a method.
  • 3. • Java exception handling is managed via five keywords: • try, • catch • throw, • throws, • finally.
  • 4. • Program statements that we want to monitor for exceptions are contained within a try block. • If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. • System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, we use the keyword throw. • Any exception that is thrown out of a method must be specified as such by a throws clause. Any code that absolutely must be executed before a method returns is put in a finally block.
  • 5. try { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionType2 } // ... finally { // block of code to be executed before try block ends } Here, ExceptionType is the type of exception that has occurred
  • 6. • All exception types are subclasses of the built-in class Throwable. Throwable is at the top of the exception class hierarchy. Immediately below Throwable are two subclasses that partition exceptions into two distinct branches. • One branch is headed by Exception. – This class is used for exceptional conditions that user programs should catch. This is also the class that you will subclass to create your own custom exception types. – There is an important subclass of Exception, called RuntimeException. Exceptions of this type are automatically defined for the programs that you write and include things such as division by zero and invalid array indexing. Exception Types
  • 7. • The other branch is topped by Error, which defines exceptions that are not expected to be caught under normal circumstances by our program. – Exceptions of type Error are used by the Java run- time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error.
  • 9. Uncaught Exceptions class Exc0 { public static void main(String args[]) { int d = 0; int a = 42 / d; } } • When the Java run-time system detects the attempt to divide by zero, it constructs a new exception object and then throws this exception. This causes the execution of Exc0 to stop, because once an exception has been thrown, it must be caught by an exception handler and dealt with immediately. • In this case, we haven’t supplied any exception handlers of our own, so the exception is caught by the default handler provided by the Java run-time system.
  • 10. • Any exception that is not caught by our program will ultimately be processed by the default handler. The default handler displays a string describing the exception, prints a stack trace from the point at which the exception occurred, and terminates the program. • Here is the output generated when this example is executed. java.lang.ArithmeticException: / by zero at Exc0.main(Exc0.java:4) • The type of the exception thrown is a subclass of Exception called ArithmeticException, which more specifically describes what type of error happened. Java supplies several built-in exception types that match the various sorts of run-time errors that can be generated. The stack trace will always show the sequence of method invocations that led up to the error.