SlideShare a Scribd company logo
1 of 18
Prepared by
V.Santhi
Assistant Professor
Department of Computer Application
Bon Secours College for Women
Thanjavur
• Due to design errors or coding errors, our
programs may fail in unexpected ways during
execution.
Exception
An exception is a condition that is caused by run
time error in the program.
 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.
 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.
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);
}
}
 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)
 Exceptions – a better error handling
 Exceptions are a mechanism that provides the
best of both worlds.
 Exceptions act similar to method return flags in
that any method may raise and exception should
it encounter an error.
 Exceptions act like global error methods in that
the exception mechanism is built into Java;
exceptions are handled at many levels in a
program, locally and/or globally.
Throwable
Exception Error
Runtime
Exception
IO Exception
1. Find the problem (Hit the exception) try
2. Inform that an error has occurred (throw the
exception) throw
3. Receive the error information (catch the
exception) catch
4. Take corrective actions (Handle the
exception)
• Java exception handling is managed via five
keywords :
try, catch, throw, throws , finally
 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
 Example
 try {
… normal program code
}
catch(Exception e) {
… exception handling code
}
 Types of Exceptions
 Examples:
▪ public void myMethod throws Exception {
▪ public void myMethod throws IOException {
▪ try { … }
catch (Exception e) { … }
▪ try { … }
catch (IOException ioe) { … }
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);
}
• It is possible to throw an exception explicitly,
using the throw statement.The general form
of throw is shown here
Syntax:
throw throwableinstance;
• 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.
Syntax:
datatype method-name(parameter-list) throws
exception-list
{
body of method;
}
• java supports another statement known as
finally statement that can be used to handle an
exception that is not caught by any of the
previous catch statements.
• It may be added immediately after the try block
or after the last catch block shown as follows
try {
statement;
} finally
{ statement;
}

More Related Content

Similar to exception-handling-in-java.ppt

Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfFurkan Furkan
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javagopalrajput11
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling ConceptsVicter Paul
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertionsphanleson
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfFacultyAnupamaAlagan
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
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 allHayomeTakele
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingSakkaravarthiS1
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception HandlingGovindanS3
 

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

Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdfJava_Exception-CheatSheet_Edureka.pdf
Java_Exception-CheatSheet_Edureka.pdf
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
 
Chapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdfChapter 5 Exception Handling (1).pdf
Chapter 5 Exception Handling (1).pdf
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
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
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Java exception
Java exception Java exception
Java exception
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 

More from SanthiNivas

Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.pptSanthiNivas
 
static methods.pptx
static methods.pptxstatic methods.pptx
static methods.pptxSanthiNivas
 
transmission media.ppt
transmission media.ppttransmission media.ppt
transmission media.pptSanthiNivas
 
Internet Basics Presentation.pptx
Internet Basics Presentation.pptxInternet Basics Presentation.pptx
Internet Basics Presentation.pptxSanthiNivas
 
Features of Java.pptx
Features of Java.pptxFeatures of Java.pptx
Features of Java.pptxSanthiNivas
 
Output Devices.pptx
Output Devices.pptxOutput Devices.pptx
Output Devices.pptxSanthiNivas
 
Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptxSanthiNivas
 
Operating System File Management Unit v.pptx
Operating System File Management Unit v.pptxOperating System File Management Unit v.pptx
Operating System File Management Unit v.pptxSanthiNivas
 
Input and Output Devices
Input and Output DevicesInput and Output Devices
Input and Output DevicesSanthiNivas
 
DDA ALGORITHM.pdf
DDA ALGORITHM.pdfDDA ALGORITHM.pdf
DDA ALGORITHM.pdfSanthiNivas
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer GraphicsSanthiNivas
 
Page Layout and Background
Page Layout and BackgroundPage Layout and Background
Page Layout and BackgroundSanthiNivas
 
3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics3-D Transformation in Computer Graphics
3-D Transformation in Computer GraphicsSanthiNivas
 
4. THREE DIMENSIONAL DISPLAY METHODS
4.	THREE DIMENSIONAL DISPLAY METHODS4.	THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODSSanthiNivas
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel DrawSanthiNivas
 

More from SanthiNivas (20)

packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.ppt
 
static methods.pptx
static methods.pptxstatic methods.pptx
static methods.pptx
 
Topologies.ppt
Topologies.pptTopologies.ppt
Topologies.ppt
 
transmission media.ppt
transmission media.ppttransmission media.ppt
transmission media.ppt
 
Internet Basics Presentation.pptx
Internet Basics Presentation.pptxInternet Basics Presentation.pptx
Internet Basics Presentation.pptx
 
Topologies.ppt
Topologies.pptTopologies.ppt
Topologies.ppt
 
Features of Java.pptx
Features of Java.pptxFeatures of Java.pptx
Features of Java.pptx
 
Output Devices.pptx
Output Devices.pptxOutput Devices.pptx
Output Devices.pptx
 
Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptx
 
Operating System File Management Unit v.pptx
Operating System File Management Unit v.pptxOperating System File Management Unit v.pptx
Operating System File Management Unit v.pptx
 
Input and Output Devices
Input and Output DevicesInput and Output Devices
Input and Output Devices
 
HTML
HTMLHTML
HTML
 
DDA ALGORITHM.pdf
DDA ALGORITHM.pdfDDA ALGORITHM.pdf
DDA ALGORITHM.pdf
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Page Layout and Background
Page Layout and BackgroundPage Layout and Background
Page Layout and Background
 
3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics3-D Transformation in Computer Graphics
3-D Transformation in Computer Graphics
 
4. THREE DIMENSIONAL DISPLAY METHODS
4.	THREE DIMENSIONAL DISPLAY METHODS4.	THREE DIMENSIONAL DISPLAY METHODS
4. THREE DIMENSIONAL DISPLAY METHODS
 
-Working with Text in Corel Draw
-Working with Text in Corel Draw-Working with Text in Corel Draw
-Working with Text in Corel Draw
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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 🔝✔️✔️
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

exception-handling-in-java.ppt

  • 1. Prepared by V.Santhi Assistant Professor Department of Computer Application Bon Secours College for Women Thanjavur
  • 2. • Due to design errors or coding errors, our programs may fail in unexpected ways during execution. Exception An exception is a condition that is caused by run time error in the program.
  • 3.  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.
  • 4.  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.
  • 5. 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); } }
  • 6.  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)
  • 7.  Exceptions – a better error handling  Exceptions are a mechanism that provides the best of both worlds.  Exceptions act similar to method return flags in that any method may raise and exception should it encounter an error.  Exceptions act like global error methods in that the exception mechanism is built into Java; exceptions are handled at many levels in a program, locally and/or globally.
  • 9. 1. Find the problem (Hit the exception) try 2. Inform that an error has occurred (throw the exception) throw 3. Receive the error information (catch the exception) catch 4. Take corrective actions (Handle the exception)
  • 10. • Java exception handling is managed via five keywords : try, catch, throw, throws , finally
  • 11.
  • 12.  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
  • 13.  Example  try { … normal program code } catch(Exception e) { … exception handling code }
  • 14.  Types of Exceptions  Examples: ▪ public void myMethod throws Exception { ▪ public void myMethod throws IOException { ▪ try { … } catch (Exception e) { … } ▪ try { … } catch (IOException ioe) { … }
  • 15. 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); }
  • 16. • It is possible to throw an exception explicitly, using the throw statement.The general form of throw is shown here Syntax: throw throwableinstance;
  • 17. • 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. Syntax: datatype method-name(parameter-list) throws exception-list { body of method; }
  • 18. • java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statements. • It may be added immediately after the try block or after the last catch block shown as follows try { statement; } finally { statement; }