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

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  Exceptionare 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 HandlingMechanism  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 objectsused 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 ThrowPoint •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  Thethrow 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  Thetype 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 catchstatement 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  Whenan 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 ExceptionCause 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