SlideShare a Scribd company logo
N.SURATHAVANI(Info
Tech)
EXCEPTION
HANDLING
NADAR SARASWATHI
COLLEGE OF ARTS AND
SCIENCE,THENI.
EXCEPTION HANDLING
FUNDAMENTALS:
A Jvava exception is an object that describes an
exceptional(that is,error) condition that has
occurred in a piece of code.
Exceptions can be generated by the Java run-
time system, or they can be manually generated
by your code.
Exceptions thrown by Java relate to
fundamental errors that violate the rules of the
Java language or the constraints of the Java
execution environment.
Key words:
 try
catch
throw
finally.
General form of an exception-
handling block:
try{
//block of code to monitor for errors
}
catch(Exception Type1 exOb){
//exception handler for ExceptionType1
}
catch(Exception Type2 exOb){
//exception handler for ExceptionType2
}
//...
finally{
//block of code to be executed after try blocks ends
}
EXCEPTION TYPES
All exception types are subclasses of the built-in
class Throwable.
Thus, Throwable is at the top of the exception
class
hierarchy.
Branches
Headed
by
exceptio
n
Topped
by Error
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
Runtime Exception.
Topped by Error:
The other branch is topped by Error, which
defines exceptions that are not excepted to
be caught under normal circumstances by
your 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.
Try catch block:
Although the default exception handler provided
by the java run-time system is useful for
debugging.
To guard against and handle a run-time
error,simply enclose the code that you want to
monitor inside a try block.
Immediately following the try block, include a
catch clause that specifies the exception type
that you wish to catch,
Example program:
class Exc2{
public static void main(String args[ ]){
int d,a;
try { // monitor a block of code.
d=0;
a=42/d;
System.out.println(“This will not be printed”);
} catch (ArithmeticException e){//catch divide-by-
zero error
System.out.println(“Division by zero”);
}
System.out.println(“After catch statement”);
}
}
THROW:
ThrowableInstance must be an object of type
Throwable or a subclass of Throwable.
Primitive types, such as int or char, as well as
non-Throwable classes, such as String and
Object, cannot be used as exceptions.
throw ThrowableInstance;
Two ways can obtain a
Throwable object:
parameter in a catch clause.
creating one with the
new operator.
It is possible for your program to throw an
exception explicity.
Throwable Instance must be an object of type
throwable.
The flow of execution will stop automatically
after
encounter the “throw keyword”.
The nearest try and catch block inspected
immediatly.
 The flow of execution stops immediately
after the throw statement; any subsequent
statements are not executed.
The nearest enclosing try bock is inspected
to
see if it has a catch statement that matches
the
type of exception.
If it does find a match, control is transferred
to
that statement.If not,then the next enclosing
Example program:
// Demonstrate throw.
Class ThrowDemo{
Static void demoproc()
{
try
{
Throw new NullPointerException(“demo”);
}
catch(NullPointerException e)
{
System.out.println(“Caught inside demoproc.”);
Throw e; // rethrow the exception
}
}
Public static void main(String args[])
{
Try
{
Demoproc();
}
catch(NullPointerException e)
{
System.out.println(“Recaught: ” +e);
}
}
}
throws
Capable of causing an exception. It didn’t
handle by
catch block.
You can do by using “throws” declaration.
Throws clause lists the types of explicitly that a
method might throw.
If a method is capable of causing an exception
that it does not handle, it must specify this
behavior so that callers of the method can guard
themselves against that exception.

More Related Content

What's hot (20)

PPTX
Exception handling in java
pooja kumari
 
PPT
Exception handling
Tata Consultancy Services
 
PPTX
L14 exception handling
teach4uin
 
PPTX
Exceptionhandling
Nuha Noor
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPTX
Java - Exception Handling
Prabhdeep Singh
 
PPTX
17 exceptions
dhrubo kayal
 
PPT
Multi catch statement
myrajendra
 
PPTX
Chap2 exception handling
raksharao
 
PPTX
Exception handling
Ardhendu Nandi
 
PDF
Built in exceptions
TharuniDiddekunta
 
PPTX
Interface andexceptions
saman Iftikhar
 
PPT
Exception handling in java
Pratik Soares
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
7.error management and exception handling
Deepak Sharma
 
PPTX
Exception handling in Java
Prasad Sawant
 
PPTX
Java exception handling
Md. Tanvir Hossain
 
PPTX
Satish training ppt
satish lariya
 
PPTX
Exception handling in java
Elizabeth alexander
 
PPT
Exception handling
M Vishnuvardhan Reddy
 
Exception handling in java
pooja kumari
 
Exception handling
Tata Consultancy Services
 
L14 exception handling
teach4uin
 
Exceptionhandling
Nuha Noor
 
Exception Handling in Java
lalithambiga kamaraj
 
Java - Exception Handling
Prabhdeep Singh
 
17 exceptions
dhrubo kayal
 
Multi catch statement
myrajendra
 
Chap2 exception handling
raksharao
 
Exception handling
Ardhendu Nandi
 
Built in exceptions
TharuniDiddekunta
 
Interface andexceptions
saman Iftikhar
 
Exception handling in java
Pratik Soares
 
Exception Handling
Reddhi Basu
 
7.error management and exception handling
Deepak Sharma
 
Exception handling in Java
Prasad Sawant
 
Java exception handling
Md. Tanvir Hossain
 
Satish training ppt
satish lariya
 
Exception handling in java
Elizabeth alexander
 
Exception handling
M Vishnuvardhan Reddy
 

Similar to Java (20)

DOCX
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
PPTX
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPT
8.Exception handling latest(MB).ppt .
happycocoman
 
PPTX
Unit-4 Java ppt for BCA Students Madras Univ
lavanyasujat1
 
PPT
Exceptions in java
JasmeetSingh326
 
PDF
16 exception handling - i
Ravindra Rathore
 
PPS
Java Exception handling
kamal kotecha
 
PPT
Exceptionhandling
DrHemlathadhevi
 
PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
PPTX
UNIT 2.pptx
EduclentMegasoftel
 
PPTX
Java exception handling
GaneshKumarKanthiah
 
PPTX
Unit 4 exceptions and threads
DevaKumari Vijay
 
PPT
Chap12
Terry Yoast
 
PDF
Java unit 11
Shipra Swati
 
PPT
Exception Handling
PRN USM
 
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
PPT
9781439035665 ppt ch11
Terry Yoast
 
PPT
Exception
Hoang Nguyen
 
MODULE5_EXCEPTION HANDLING.docx
VeerannaKotagi1
 
Exception handling, Stream Classes, Multithread Programming
Prabu U
 
Exception Handling in JAVA
SURIT DATTA
 
8.Exception handling latest(MB).ppt .
happycocoman
 
Unit-4 Java ppt for BCA Students Madras Univ
lavanyasujat1
 
Exceptions in java
JasmeetSingh326
 
16 exception handling - i
Ravindra Rathore
 
Java Exception handling
kamal kotecha
 
Exceptionhandling
DrHemlathadhevi
 
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
UNIT 2.pptx
EduclentMegasoftel
 
Java exception handling
GaneshKumarKanthiah
 
Unit 4 exceptions and threads
DevaKumari Vijay
 
Chap12
Terry Yoast
 
Java unit 11
Shipra Swati
 
Exception Handling
PRN USM
 
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
roshansingh0407
 
9781439035665 ppt ch11
Terry Yoast
 
Exception
Hoang Nguyen
 
Ad

More from SangeethaSasi1 (20)

PPT
L4 multiplexing & multiple access 16
SangeethaSasi1
 
PPT
Image processing using matlab
SangeethaSasi1
 
PPTX
Mc ppt
SangeethaSasi1
 
PPTX
Mc ppt
SangeethaSasi1
 
PPTX
Dip pppt
SangeethaSasi1
 
PPTX
Web techh
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Vani wt
SangeethaSasi1
 
PPTX
Vani dbms
SangeethaSasi1
 
PPTX
Hema wt (1)
SangeethaSasi1
 
PPTX
Hema rdbms
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Dbms
SangeethaSasi1
 
PPTX
Vani
SangeethaSasi1
 
PPTX
Hema se
SangeethaSasi1
 
PPTX
Software
SangeethaSasi1
 
PPTX
Operating system
SangeethaSasi1
 
PPTX
Dataminng
SangeethaSasi1
 
PPTX
System calls
SangeethaSasi1
 
L4 multiplexing & multiple access 16
SangeethaSasi1
 
Image processing using matlab
SangeethaSasi1
 
Mc ppt
SangeethaSasi1
 
Mc ppt
SangeethaSasi1
 
Dip pppt
SangeethaSasi1
 
Web techh
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Vani wt
SangeethaSasi1
 
Vani dbms
SangeethaSasi1
 
Hema wt (1)
SangeethaSasi1
 
Hema rdbms
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Hema se
SangeethaSasi1
 
Software
SangeethaSasi1
 
Operating system
SangeethaSasi1
 
Dataminng
SangeethaSasi1
 
System calls
SangeethaSasi1
 
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PDF
BÀI TáșŹP TEST BỔ TRỹ THEO Tá»ȘNG CHỊ ĐỀ CỊA Tá»ȘNG UNIT KÈM BÀI TáșŹP NGHE - TIáșŸNG A...
Nguyen Thanh Tu Collection
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
John Keats introduction and list of his important works
vatsalacpr
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
BÀI TáșŹP TEST BỔ TRỹ THEO Tá»ȘNG CHỊ ĐỀ CỊA Tá»ȘNG UNIT KÈM BÀI TáșŹP NGHE - TIáșŸNG A...
Nguyen Thanh Tu Collection
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Virus sequence retrieval from NCBI database
yamunaK13
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 

Java

  • 2. EXCEPTION HANDLING FUNDAMENTALS: A Jvava exception is an object that describes an exceptional(that is,error) condition that has occurred in a piece of code. Exceptions can be generated by the Java run- time system, or they can be manually generated by your code. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment.
  • 4. General form of an exception- handling block: try{ //block of code to monitor for errors } catch(Exception Type1 exOb){ //exception handler for ExceptionType1 } catch(Exception Type2 exOb){ //exception handler for ExceptionType2 } //... finally{ //block of code to be executed after try blocks ends }
  • 5. EXCEPTION TYPES All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy. Branches Headed by exceptio n Topped by Error
  • 6. 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 Runtime Exception.
  • 7. Topped by Error: The other branch is topped by Error, which defines exceptions that are not excepted to be caught under normal circumstances by your 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.
  • 8. Try catch block: Although the default exception handler provided by the java run-time system is useful for debugging. To guard against and handle a run-time error,simply enclose the code that you want to monitor inside a try block. Immediately following the try block, include a catch clause that specifies the exception type that you wish to catch,
  • 9. Example program: class Exc2{ public static void main(String args[ ]){ int d,a; try { // monitor a block of code. d=0; a=42/d; System.out.println(“This will not be printed”); } catch (ArithmeticException e){//catch divide-by- zero error System.out.println(“Division by zero”); } System.out.println(“After catch statement”); } }
  • 10. THROW: ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Primitive types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. throw ThrowableInstance;
  • 11. Two ways can obtain a Throwable object: parameter in a catch clause. creating one with the new operator.
  • 12. It is possible for your program to throw an exception explicity. Throwable Instance must be an object of type throwable. The flow of execution will stop automatically after encounter the “throw keyword”. The nearest try and catch block inspected immediatly.
  • 13.  The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. The nearest enclosing try bock is inspected to see if it has a catch statement that matches the type of exception. If it does find a match, control is transferred to that statement.If not,then the next enclosing
  • 14. Example program: // Demonstrate throw. Class ThrowDemo{ Static void demoproc() { try { Throw new NullPointerException(“demo”); } catch(NullPointerException e) { System.out.println(“Caught inside demoproc.”); Throw e; // rethrow the exception
  • 15. } } Public static void main(String args[]) { Try { Demoproc(); } catch(NullPointerException e) { System.out.println(“Recaught: ” +e); } } }
  • 16. throws Capable of causing an exception. It didn’t handle by catch block. You can do by using “throws” declaration. Throws clause lists the types of explicitly that a method might throw. If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception.