SlideShare a Scribd company logo
1 of 9
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Exception handling
Exception: are those run time error which can be managed programmatically. The process of
managing error in application is called exception handling.
The purpose of exception handling is to avoid the abnormal program termination.
In java each run time error is represented as an object. family of classes is provided bu sun micro
system to represent run time error. Top most member of the family is a class named throwable
.it has two subclasses.
Commonly used subclasses of Exception:
1. Arithmetic Exception:
Represent invalid operations. Such as division by zero.
2. ArrayIndexoutofBound Exception:
Attempt to refer non existence array element.
3. Number format exception:
Represents an attempt to convert nonnumeric string in to a number.
4. Null pointer exception:
THROWABLE
EXCEPTION
Can b e managed
programmatically
ERROR
(Can not be
managed
programmatically
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
It represents an attempt to use a null containing reference variable for referencing object
members.
Commonly used subclass of Error:
1. Virtual machine error:
Represents malfunctioning of JRE.
2. Stack Overflow Error:
Represents lack of space in stack.
3. No Class definition found Error:
Represents inability to locate a referenced class.
4. No such Method error:
Represents inability to locate a referenced method etc.
Following keyword provided by Java to facilitate exception handling:
 Try
 Catch
 Throw
 Throws
 Finally
Try keyword:try keyword is used to make a block of statement as error prone.try keyword is
never used alone .it is used with either catch or finally or both.
Catch: catch keyword is used to define a error handler block.it contains statement which are to be
executed when a run time error occurs.
Finally:finally keyword is used to define a block of statement which are to be executed before
normal or abnormal termination.
Try
{
Error prone statement
}
Catch(Exception object)
{
Error handling statement
}
Try
{
;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;
}
Finally
{
Statement which are to be
executed before termination of
Try
{[
;;;;;;;;;;;;;;;;;;;;
‘’’’’’’’’’’’’’’’’’’’’
;;;;;;;;;;;;;;;;;;;
}
Catch(Exception object)
{
;;;;;;;;;;;;;;;;;;;
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
program
}
;;;;;;;;;;;;;;;;;;;;;;
}
Finally
{
;;;;;;;;;;;;;;;;;;;;;;;
‘;;;;;;;;;;;;;;;;;;;;;;;;
}
Note:more than one catch block can be associated to a try block.but only one finally block can
be used.
Ex;:two number are received as command line argument first no is divided by second number
and result is displayed on console.
Class Divide
{
Public static void main(String arr[])
{
Try
{
Int a=Integer.parseInt(arr[0]);
Int b=Integer.parseInt(arr[1]);
Int c =a/b;
S.O.P(“Result=”+c);
}
Catch(ArithmaticException e)
{
S.O.P(“second number must be non zero”);
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
}
Catch(ArrayIndexoutofBoundException e)
{
S.O.P(“two argument must be provided”);
}
Catch(NumberFormatException e)
{
S.O.P(“argument must be numeric”);
}
}
}
Ex:Exception generated in method and caught in another.
Class A
{
Void getException()
{
Int [] num=new int[4];
S.O.P(“before Execution of exception”);
Num[7]=4;
S.O.P(“cannot display”);
}
}
Class Test
{
p.s.v.m(String arr[])
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
{
Try
{
A.getException();
}catch(ArrayIndexOutOfBoundsException ex)
{
S.O.P(“index is out of Bound”);
}
S.O.P(“after the Exception caught”);
}
}
Block diagram of generalized exceptions:
Only exit from try block resulting an exception can transfer control to catch block. A catch
block can only catch the thrown exception.
Throw: throw keyword is used for throwing exception explicitly. it is used for following:
 For throwing user defined exception.
 For customizing the description of predefined exception.
 For re throwing exceptions.
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Ex. Program below show the use of throw. Exception is thrown from the point where it is
generated.
Class Divide
{
public static void main(String arr[])
{
Try
{
If(arr.length<2)
{
throw(new Exception (“two argument must be provided”);
int a=Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);
if(b==0)
throw(new Exception(“second argument should be non zero”);
int c=a/b;
S.O.P(“Result=”+c);
}
Catch(Exception e)
{
S.O.P (e);
}
}
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
Finally Block: if the try block is executed, then finally block is guaranteed to be executed
regardless of whether any catch block execute or not. Since finally block is always executed
before control transfer to its final destination. It can be used to specify any clean up code.
Example :
Class Average
{
Public static void main(string arr[])
{
printAvg(100,0);
S.O.P(“exit”);
}
Public static void printAvg( int sum,int num)
{
Try
{
Int avg=CAvg(sum,num);
S.O.P(“Sum avg=”+sum+”/”+num+”=”+avg);
}
Catch(Arithmeticexception e)
{
e.printStackTrace();
S.O.P(“exception handled in printAvg”);
}
Finally
{
S.O.P(“finally done”);
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
}
Throws:
If any method creates exception that it does not handle. So it specify a behavior so that caller of
the method can guard themselves against that exception. So we can include throws in the method
declaration.
A throws clause list type of exception that a method might throw.
E.g
Type methodname(Parameter) throws Exception-list
{
}
In exception list you can use comma to separate the exception types:
Class ThrowsDemo
{
Static void throwOne ()throws illegalAccessException
{
S.O.P(“inside throw one”);
Throw new illegalAccessException (“demo”);}
Public static void main(String arr[])
{
Try
{
throwOne();
}catch(illegalAccessException e)
{
Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com
S.O.P(“caught “+e);
}
}
}
Difference between throw and throws:
Both can be used together in a class but their behavior is different.
Throws is used to indicate or enforce the caller method to handle that checked exception or
caller method can also write throws clause to propagate the exception handling responsibility to
its caller method.
Throw is used to write throw statement which throws the exceptions
JVM can throw exception while executing the program.

More Related Content

What's hot (19)

Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Control structure
Control structureControl structure
Control structure
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
C++ ala
C++ alaC++ ala
C++ ala
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similar to Exception handling

Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxNagaraju Pamarthi
 
Exception Hnadling java programming language
Exception Hnadling  java programming languageException Hnadling  java programming language
Exception Hnadling java programming languageushakiranv110
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliabilitymcollison
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024kashyapneha2809
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 

Similar to Exception handling (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Exception Hnadling java programming language
Exception Hnadling  java programming languageException Hnadling  java programming language
Exception Hnadling java programming language
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
Exceptions
ExceptionsExceptions
Exceptions
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
 
Exception handling
Exception handlingException handling
Exception handling
 
Java unit3
Java unit3Java unit3
Java unit3
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
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
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
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
 
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
 
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
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
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
 
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🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
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
 
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
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
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
 
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
 
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
 

Exception handling

  • 1. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com Exception handling Exception: are those run time error which can be managed programmatically. The process of managing error in application is called exception handling. The purpose of exception handling is to avoid the abnormal program termination. In java each run time error is represented as an object. family of classes is provided bu sun micro system to represent run time error. Top most member of the family is a class named throwable .it has two subclasses. Commonly used subclasses of Exception: 1. Arithmetic Exception: Represent invalid operations. Such as division by zero. 2. ArrayIndexoutofBound Exception: Attempt to refer non existence array element. 3. Number format exception: Represents an attempt to convert nonnumeric string in to a number. 4. Null pointer exception: THROWABLE EXCEPTION Can b e managed programmatically ERROR (Can not be managed programmatically
  • 2. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com It represents an attempt to use a null containing reference variable for referencing object members. Commonly used subclass of Error: 1. Virtual machine error: Represents malfunctioning of JRE. 2. Stack Overflow Error: Represents lack of space in stack. 3. No Class definition found Error: Represents inability to locate a referenced class. 4. No such Method error: Represents inability to locate a referenced method etc. Following keyword provided by Java to facilitate exception handling:  Try  Catch  Throw  Throws  Finally Try keyword:try keyword is used to make a block of statement as error prone.try keyword is never used alone .it is used with either catch or finally or both. Catch: catch keyword is used to define a error handler block.it contains statement which are to be executed when a run time error occurs. Finally:finally keyword is used to define a block of statement which are to be executed before normal or abnormal termination. Try { Error prone statement } Catch(Exception object) { Error handling statement } Try { ;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;; } Finally { Statement which are to be executed before termination of Try {[ ;;;;;;;;;;;;;;;;;;;; ‘’’’’’’’’’’’’’’’’’’’’ ;;;;;;;;;;;;;;;;;;; } Catch(Exception object) { ;;;;;;;;;;;;;;;;;;;
  • 3. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com program } ;;;;;;;;;;;;;;;;;;;;;; } Finally { ;;;;;;;;;;;;;;;;;;;;;;; ‘;;;;;;;;;;;;;;;;;;;;;;;; } Note:more than one catch block can be associated to a try block.but only one finally block can be used. Ex;:two number are received as command line argument first no is divided by second number and result is displayed on console. Class Divide { Public static void main(String arr[]) { Try { Int a=Integer.parseInt(arr[0]); Int b=Integer.parseInt(arr[1]); Int c =a/b; S.O.P(“Result=”+c); } Catch(ArithmaticException e) { S.O.P(“second number must be non zero”);
  • 4. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com } Catch(ArrayIndexoutofBoundException e) { S.O.P(“two argument must be provided”); } Catch(NumberFormatException e) { S.O.P(“argument must be numeric”); } } } Ex:Exception generated in method and caught in another. Class A { Void getException() { Int [] num=new int[4]; S.O.P(“before Execution of exception”); Num[7]=4; S.O.P(“cannot display”); } } Class Test { p.s.v.m(String arr[])
  • 5. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com { Try { A.getException(); }catch(ArrayIndexOutOfBoundsException ex) { S.O.P(“index is out of Bound”); } S.O.P(“after the Exception caught”); } } Block diagram of generalized exceptions: Only exit from try block resulting an exception can transfer control to catch block. A catch block can only catch the thrown exception. Throw: throw keyword is used for throwing exception explicitly. it is used for following:  For throwing user defined exception.  For customizing the description of predefined exception.  For re throwing exceptions.
  • 6. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com Ex. Program below show the use of throw. Exception is thrown from the point where it is generated. Class Divide { public static void main(String arr[]) { Try { If(arr.length<2) { throw(new Exception (“two argument must be provided”); int a=Integer.parseInt(arr[0]); int b = Integer.parseInt(arr[1]); if(b==0) throw(new Exception(“second argument should be non zero”); int c=a/b; S.O.P(“Result=”+c); } Catch(Exception e) { S.O.P (e); } }
  • 7. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com Finally Block: if the try block is executed, then finally block is guaranteed to be executed regardless of whether any catch block execute or not. Since finally block is always executed before control transfer to its final destination. It can be used to specify any clean up code. Example : Class Average { Public static void main(string arr[]) { printAvg(100,0); S.O.P(“exit”); } Public static void printAvg( int sum,int num) { Try { Int avg=CAvg(sum,num); S.O.P(“Sum avg=”+sum+”/”+num+”=”+avg); } Catch(Arithmeticexception e) { e.printStackTrace(); S.O.P(“exception handled in printAvg”); } Finally { S.O.P(“finally done”);
  • 8. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com } Throws: If any method creates exception that it does not handle. So it specify a behavior so that caller of the method can guard themselves against that exception. So we can include throws in the method declaration. A throws clause list type of exception that a method might throw. E.g Type methodname(Parameter) throws Exception-list { } In exception list you can use comma to separate the exception types: Class ThrowsDemo { Static void throwOne ()throws illegalAccessException { S.O.P(“inside throw one”); Throw new illegalAccessException (“demo”);} Public static void main(String arr[]) { Try { throwOne(); }catch(illegalAccessException e) {
  • 9. Vishal Choudhary-Jaipur National universityjaipur. Email:vishalhim@yahoo.com S.O.P(“caught “+e); } } } Difference between throw and throws: Both can be used together in a class but their behavior is different. Throws is used to indicate or enforce the caller method to handle that checked exception or caller method can also write throws clause to propagate the exception handling responsibility to its caller method. Throw is used to write throw statement which throws the exceptions JVM can throw exception while executing the program.