0% found this document useful (0 votes)
32 views

Exception Handling

The document discusses exception handling in programming. It defines different types of errors like syntax errors, semantic errors, and logical errors. It also defines different types of exceptions like checked exceptions and unchecked exceptions in Java. Finally, it explains the exception handling mechanism using try, throw, catch, and finally keywords. The try block contains code that might throw an exception. The catch block catches the exception if thrown and the finally block executes regardless of whether an exception was thrown or not.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Exception Handling

The document discusses exception handling in programming. It defines different types of errors like syntax errors, semantic errors, and logical errors. It also defines different types of exceptions like checked exceptions and unchecked exceptions in Java. Finally, it explains the exception handling mechanism using try, throw, catch, and finally keywords. The try block contains code that might throw an exception. The catch block catches the exception if thrown and the finally block executes regardless of whether an exception was thrown or not.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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 Try Block
Detects & throws and Exception
Throws
Exception
Object

Catch Block
Catch & handle the 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

Invoke function

Throw Point Try Block Catch Block


•Function that cause •Invokes a function that •Catch & handle the
an Exception contains an Exception Exception

Throws Exception
Throwing Mechanism
 The throw statement can have one of the following 3 forms
Throw new Throwable subclass;
 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.
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy