SlideShare a Scribd company logo
1 of 16
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

What's hot (20)

Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Satish training ppt
Satish training pptSatish training ppt
Satish training ppt
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception handling
Exception handling Exception handling
Exception handling
 

Similar to Java

MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxVeerannaKotagi1
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingPrabu U
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
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
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threadsDevaKumari Vijay
 
Exception Handling
Exception HandlingException Handling
Exception HandlingPRN USM
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11Terry Yoast
 

Similar to Java (20)

MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
Exception handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread ProgrammingException handling, Stream Classes, Multithread Programming
Exception handling, Stream Classes, Multithread Programming
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Chap12
Chap12Chap12
Chap12
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
9781439035665 ppt ch11
9781439035665 ppt ch119781439035665 ppt ch11
9781439035665 ppt ch11
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 

More from SangeethaSasi1 (20)

L4 multiplexing & multiple access 16
L4 multiplexing & multiple access 16L4 multiplexing & multiple access 16
L4 multiplexing & multiple access 16
 
Image processing using matlab
Image processing using matlab Image processing using matlab
Image processing using matlab
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Dip pppt
Dip ppptDip pppt
Dip pppt
 
Web techh
Web techhWeb techh
Web techh
 
Web tech
Web techWeb tech
Web tech
 
Vani wt
Vani wtVani wt
Vani wt
 
Vani dbms
Vani dbmsVani dbms
Vani dbms
 
Hema wt (1)
Hema wt (1)Hema wt (1)
Hema wt (1)
 
Hema rdbms
Hema rdbmsHema rdbms
Hema rdbms
 
Web tech
Web techWeb tech
Web tech
 
Web tech
Web techWeb tech
Web tech
 
Dbms
DbmsDbms
Dbms
 
Vani
VaniVani
Vani
 
Hema se
Hema seHema se
Hema se
 
Software
SoftwareSoftware
Software
 
Operating system
Operating systemOperating system
Operating system
 
Dataminng
DataminngDataminng
Dataminng
 
System calls
System callsSystem calls
System calls
 

Recently uploaded

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

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.