SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
BY
K.LALITHAMBIGA
II –Msc (CS&IT )
Department of CS&IT
NS College of Arts and Science,
Theni.
Exception Handling
 Exception are run time anomalies or unusual condition that a program may
encounter during execution.
Type of Program errors
We distinguish between the following types of errors:
• Syntax error: Errors due to the fact that the syntax of the language
is not respected.
• Semantic error: Errors due to an improper use of program
statements.
• Logical errors: Errors due to the fact that the specification is not
respected.
From the point of view of when errors are detected
 Compile time error: Syntax errors & Static semantic errors indicated
by the compiler.
 Runtime errors: Dynamic semantic errors, that errors, that cannot be
detected by the compiler(debugging).
Exception Handling
Runtime Errors
 Division by zero
 Access to an array outside of its bounds
 Running out of memory
 Running out of disk space
 It was not a part of original C++
 It is a new feature added to ANSI C++
Exception are Two kinds:
 Synchronous Exception
 Out of rage
 Over flow
 Asynchronous Exception
 Error that are caused by causes beyond the control of the program.
 Keyboard interrupts.
In C++ only Synchronous exception can be handled.
Exception Handling
Exception Handling Mechanism
 Find the problem (Hit the Exception)
 Inform that an error has occurred (Throw the Exception)
 Receive the error information (Catch the Exception)
 Take corrective action (Handle the Exception)
Exception in Java can be categorized into two types:
Checked Exception:
 These Exceptions are explicitly handled in the code itself with the help of
try-catch blocks.
 Checked Exceptions are extended from the java.lang.Exceptionclass
UnChecked Exception:
 These Exception are not essentially handled in the program code; instead
the JVM handles such exceptions.
 Unchecked Exceptions are extended from the
java.lang.RuntimeException class
Exception Handling Mechanism
 It is basically build upon three keywords.
 Try
 Throw
 Catch
Throws
Exception
Object
Catch Block
Catch & handle the Exception
Try Block
Detects & throws and Exception
Exception Handling Mechanism
 The Keyword Try is used to preface a block of statements which
may generate exceptions.
 When an exception is detected, it is Thrown using a throw
statement in the try block.
 A Catch block defined by the Keyword ‘catch’ catches the
exception and handles it appropriately.
 The catch block that catches an exception must immediately
follow the try block that throws the exception.
Exception Handling Mechanism
try
{
… //Block of statements
throw exception; //which detect and
… //throws an exception
}
catch(type arg) //catch exception
{
… //Block of statement
… //that handles the exception
}
Exception are objects used to transmit information about a
problem.
If the type of the object thrown matches the arg type in the
catch statement, the catch block is executed.
If they do not match, the program is aborted using the abort()
function (default).
Exception are thrown by functions that are invoked from within
the try blocks.
The point at which the throw is executed is called throw point.
Once an exception is thrown to the catch block, control cannot
return to the thrown point
Exception Handling Mechanism
Throw Point
•Function that cause
an Exception
Catch Block
•Catch & handle the
Exception
Try Block
•Invokes a function that
contains an Exception
Throws Exception
Invoke function
Throwing Mechanism
 The throw statement can have one of the following 3 forms
 Throw(exception)
 Throw exception
 Throw //used to re-throw a exception
• The operand object exception can be of any type, including
constant.
• It is also possible to throw an object not intended for error
handling.
• Throw point can be in a deeply nested scope within a try
block or in a deeply nested function call.
• In any case, control is transferred to the catch statement.
Throw new Throwable subclass;
Catching Mechanism
 The type indicates the type of exception the catch block handles.
 The parameter arg is an optional parameter name.
 The catch statement catches an exception whose type matches with
the type of the catch argument.
 If the parameter in the catch statement is name, then the parameter
can be used in the exception handling code.
 If a catch statement does not match the exception it is skipped.
 More than one catch statement can be associated with a try block.
Catch(type arg)
{
….
….
….
}
Catching Mechanism
Multiple catch statement
Try {
statement; //generates an exception
}
Catch(type 1 arg) {
statement; //catch block 1
}
Catch(type 2 arg){
statement; //catch block 2
}
…
...
Catch(type N arg) {
statement; //catch block N
}
Catching Mechanism
 When an exception is thrown, the exception handlers are
searched in order for a match.
 The first handler that yields a match is executed.
 If several catch statement matches the type of an
exception the first handler that matches the exception type
is executed.
 Catch all exception
Catch(….)
{
//statement for processing all exception
}
Using Finally statement
Try {
Try-block }
Catch(Exception class 1 e) {
catch-block }
Catch(Exception class 2 e) {
catch-block }
…
Finally{
Finally-block }
Using Finally statement
 Try-block: sequence of statements that will be executed
under the control of the following catch clauses.
 Catch-block: Sequence of statements that will be executed
if a statement in the try-block generates an exception of
the type specified in the corresponding catch clause.
 Finally-block: Sequence of statements that will be always
executed (both in the case where the try-block is executed
without exceptions, and in the case where a catch-block is
executed to catch an exception).
Common Java Exception
Exception Cause of Exception
Arithmetic Exception Math errors such as division by zero
ArrayIndexOutofBoundsException Bad array indexes
ArrayStoreException When a program tries to store the wrong type of data in
an array
FileNotFoundException Attempt to access a non existent file
IOException General I/O failures, such as inability to read from a file
NullPointerException Referencing a null object
NumberFormatException When a conversion between strings & number fails
OutOfMemoryException When there’s not enough memory to allocate a new
object
SecurityException When an applet tries to perform an action not allowed by
the browser’s security setting
StackOverFlowException When the system runs out of stack space
StringIndexOutOfBoundException When a program attempts to access a nonexistent
character position in a string
Exception Handling in Java

More Related Content

What's hot (20)

PPTX
Java exception handling
BHUVIJAYAVELU
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPT
Exception handling
M Vishnuvardhan Reddy
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Packages in java
Elizabeth alexander
 
PPT
Looping statements in Java
Jin Castor
 
PDF
Exception Handling in Java
Java2Blog
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Java program structure
shalinikarunakaran1
 
PPT
C# Exceptions Handling
sharqiyem
 
PDF
Java variable types
Soba Arjun
 
PPS
Introduction to class in java
kamal kotecha
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Event Handling in java
Google
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
Java exception handling
BHUVIJAYAVELU
 
Exception Handling
Reddhi Basu
 
Method overloading
Lovely Professional University
 
Control statements in java
Madishetty Prathibha
 
Exception handling
M Vishnuvardhan Reddy
 
Java threads
Prabhakaran V M
 
Packages in java
Elizabeth alexander
 
Looping statements in Java
Jin Castor
 
Exception Handling in Java
Java2Blog
 
Inner classes in java
PhD Research Scholar
 
Java program structure
shalinikarunakaran1
 
C# Exceptions Handling
sharqiyem
 
Java variable types
Soba Arjun
 
Introduction to class in java
kamal kotecha
 
Exception handling c++
Jayant Dalvi
 
Applets in java
Wani Zahoor
 
Event Handling in java
Google
 
Inheritance in java
RahulAnanda1
 
Control Statements in Java
Niloy Saha
 
this keyword in Java.pptx
ParvizMirzayev2
 

Similar to Exception Handling in Java (20)

PPTX
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
PDF
Exception handling basic
TharuniDiddekunta
 
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
PPT
Exceptionhandling
DrHemlathadhevi
 
PPTX
Exception Handling.pptx
primevideos176
 
PPTX
Exceptions overview
Bharath K
 
PPTX
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
Java SE 11 Exception Handling
Ashwin Shiv
 
PPT
Exception handling
Tata Consultancy Services
 
PPTX
Exception handling in java
Elizabeth alexander
 
PPTX
Interface andexceptions
saman Iftikhar
 
PPTX
Exception handling in java
Kavitha713564
 
ODP
Exception handling in java
priyankazope
 
PPT
Exceptions
motthu18
 
PPT
Chap12
Terry Yoast
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PDF
Lesson 8 Handling Exceptions and Errors.pdf
Fethulmubin
 
PPT
Md07 exceptions&assertion
Rakesh Madugula
 
Module 4.pptxModule 4.pptxModuModule 4.pptxModule 4.pptxle 4.pptx
mcaajiet25
 
Exception handling basic
TharuniDiddekunta
 
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exceptionhandling
DrHemlathadhevi
 
Exception Handling.pptx
primevideos176
 
Exceptions overview
Bharath K
 
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
UNIT III 2021R.pptx
RDeepa9
 
UNIT III 2021R.pptx
RDeepa9
 
Java SE 11 Exception Handling
Ashwin Shiv
 
Exception handling
Tata Consultancy Services
 
Exception handling in java
Elizabeth alexander
 
Interface andexceptions
saman Iftikhar
 
Exception handling in java
Kavitha713564
 
Exception handling in java
priyankazope
 
Exceptions
motthu18
 
Chap12
Terry Yoast
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Lesson 8 Handling Exceptions and Errors.pdf
Fethulmubin
 
Md07 exceptions&assertion
Rakesh Madugula
 
Ad

More from lalithambiga kamaraj (20)

PPTX
Firewall in Network Security
lalithambiga kamaraj
 
PPTX
Data Compression in Multimedia
lalithambiga kamaraj
 
PPTX
Data CompressionMultimedia
lalithambiga kamaraj
 
PPTX
Digital Audio in Multimedia
lalithambiga kamaraj
 
PPTX
Network Security: Physical security
lalithambiga kamaraj
 
PPTX
Graphs in Data Structure
lalithambiga kamaraj
 
PPTX
Package in Java
lalithambiga kamaraj
 
PPTX
Data structure
lalithambiga kamaraj
 
PPTX
Digital Image Processing
lalithambiga kamaraj
 
PPTX
Digital Image Processing
lalithambiga kamaraj
 
PPTX
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
PPTX
Datamining
lalithambiga kamaraj
 
PPTX
Digital Components
lalithambiga kamaraj
 
PPTX
Deadlocks in operating system
lalithambiga kamaraj
 
PPTX
Io management disk scheduling algorithm
lalithambiga kamaraj
 
PPTX
Recovery system
lalithambiga kamaraj
 
PPTX
File management
lalithambiga kamaraj
 
PPTX
Preprocessor
lalithambiga kamaraj
 
PPTX
Inheritance
lalithambiga kamaraj
 
PPTX
Managing console of I/o operations & working with files
lalithambiga kamaraj
 
Firewall in Network Security
lalithambiga kamaraj
 
Data Compression in Multimedia
lalithambiga kamaraj
 
Data CompressionMultimedia
lalithambiga kamaraj
 
Digital Audio in Multimedia
lalithambiga kamaraj
 
Network Security: Physical security
lalithambiga kamaraj
 
Graphs in Data Structure
lalithambiga kamaraj
 
Package in Java
lalithambiga kamaraj
 
Data structure
lalithambiga kamaraj
 
Digital Image Processing
lalithambiga kamaraj
 
Digital Image Processing
lalithambiga kamaraj
 
Estimating Software Maintenance Costs
lalithambiga kamaraj
 
Digital Components
lalithambiga kamaraj
 
Deadlocks in operating system
lalithambiga kamaraj
 
Io management disk scheduling algorithm
lalithambiga kamaraj
 
Recovery system
lalithambiga kamaraj
 
File management
lalithambiga kamaraj
 
Preprocessor
lalithambiga kamaraj
 
Managing console of I/o operations & working with files
lalithambiga kamaraj
 
Ad

Recently uploaded (20)

PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
infertility, types,causes, impact, and management
Ritu480198
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Introduction presentation of the patentbutler tool
MIPLM
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 

Exception Handling in Java

  • 1. BY K.LALITHAMBIGA II –Msc (CS&IT ) Department of CS&IT NS College of Arts and Science, Theni.
  • 2. Exception Handling  Exception are run time anomalies or unusual condition that a program may encounter during execution. Type of Program errors We distinguish between the following types of errors: • Syntax error: Errors due to the fact that the syntax of the language is not respected. • Semantic error: Errors due to an improper use of program statements. • Logical errors: Errors due to the fact that the specification is not respected. From the point of view of when errors are detected  Compile time error: Syntax errors & Static semantic errors indicated by the compiler.  Runtime errors: Dynamic semantic errors, that errors, that cannot be detected by the compiler(debugging).
  • 3. Exception Handling Runtime Errors  Division by zero  Access to an array outside of its bounds  Running out of memory  Running out of disk space  It was not a part of original C++  It is a new feature added to ANSI C++ Exception are Two kinds:  Synchronous Exception  Out of rage  Over flow  Asynchronous Exception  Error that are caused by causes beyond the control of the program.  Keyboard interrupts. In C++ only Synchronous exception can be handled.
  • 4. Exception Handling Exception Handling Mechanism  Find the problem (Hit the Exception)  Inform that an error has occurred (Throw the Exception)  Receive the error information (Catch the Exception)  Take corrective action (Handle the Exception) Exception in Java can be categorized into two types: Checked Exception:  These Exceptions are explicitly handled in the code itself with the help of try-catch blocks.  Checked Exceptions are extended from the java.lang.Exceptionclass UnChecked Exception:  These Exception are not essentially handled in the program code; instead the JVM handles such exceptions.  Unchecked Exceptions are extended from the java.lang.RuntimeException class
  • 5. Exception Handling Mechanism  It is basically build upon three keywords.  Try  Throw  Catch Throws Exception Object Catch Block Catch & handle the Exception Try Block Detects & throws and Exception
  • 6. Exception Handling Mechanism  The Keyword Try is used to preface a block of statements which may generate exceptions.  When an exception is detected, it is Thrown using a throw statement in the try block.  A Catch block defined by the Keyword ‘catch’ catches the exception and handles it appropriately.  The catch block that catches an exception must immediately follow the try block that throws the exception.
  • 7. Exception Handling Mechanism try { … //Block of statements throw exception; //which detect and … //throws an exception } catch(type arg) //catch exception { … //Block of statement … //that handles the exception }
  • 8. Exception are objects used to transmit information about a problem. If the type of the object thrown matches the arg type in the catch statement, the catch block is executed. If they do not match, the program is aborted using the abort() function (default). Exception are thrown by functions that are invoked from within the try blocks. The point at which the throw is executed is called throw point. Once an exception is thrown to the catch block, control cannot return to the thrown point
  • 9. Exception Handling Mechanism Throw Point •Function that cause an Exception Catch Block •Catch & handle the Exception Try Block •Invokes a function that contains an Exception Throws Exception Invoke function
  • 10. Throwing Mechanism  The throw statement can have one of the following 3 forms  Throw(exception)  Throw exception  Throw //used to re-throw a exception • The operand object exception can be of any type, including constant. • It is also possible to throw an object not intended for error handling. • Throw point can be in a deeply nested scope within a try block or in a deeply nested function call. • In any case, control is transferred to the catch statement. Throw new Throwable subclass;
  • 11. Catching Mechanism  The type indicates the type of exception the catch block handles.  The parameter arg is an optional parameter name.  The catch statement catches an exception whose type matches with the type of the catch argument.  If the parameter in the catch statement is name, then the parameter can be used in the exception handling code.  If a catch statement does not match the exception it is skipped.  More than one catch statement can be associated with a try block. Catch(type arg) { …. …. …. }
  • 12. Catching Mechanism Multiple catch statement Try { statement; //generates an exception } Catch(type 1 arg) { statement; //catch block 1 } Catch(type 2 arg){ statement; //catch block 2 } … ... Catch(type N arg) { statement; //catch block N }
  • 13. Catching Mechanism  When an exception is thrown, the exception handlers are searched in order for a match.  The first handler that yields a match is executed.  If several catch statement matches the type of an exception the first handler that matches the exception type is executed.  Catch all exception Catch(….) { //statement for processing all exception }
  • 14. Using Finally statement Try { Try-block } Catch(Exception class 1 e) { catch-block } Catch(Exception class 2 e) { catch-block } … Finally{ Finally-block }
  • 15. Using Finally statement  Try-block: sequence of statements that will be executed under the control of the following catch clauses.  Catch-block: Sequence of statements that will be executed if a statement in the try-block generates an exception of the type specified in the corresponding catch clause.  Finally-block: Sequence of statements that will be always executed (both in the case where the try-block is executed without exceptions, and in the case where a catch-block is executed to catch an exception).
  • 16. Common Java Exception Exception Cause of Exception Arithmetic Exception Math errors such as division by zero ArrayIndexOutofBoundsException Bad array indexes ArrayStoreException When a program tries to store the wrong type of data in an array FileNotFoundException Attempt to access a non existent file IOException General I/O failures, such as inability to read from a file NullPointerException Referencing a null object NumberFormatException When a conversion between strings & number fails OutOfMemoryException When there’s not enough memory to allocate a new object SecurityException When an applet tries to perform an action not allowed by the browser’s security setting StackOverFlowException When the system runs out of stack space StringIndexOutOfBoundException When a program attempts to access a nonexistent character position in a string