Unit3-Part1 (Exception Handling)
Unit3-Part1 (Exception Handling)
UNIT-3
2
DEALING WITH ERRORS
All syntax errors will be detected and displayed by the java compiler and
therefore these errors are known as compile-time errors.
most of the errors are due to typing mistakes. The most common problems
are:
missing semicolons
missing or mismatch of brackets in classes and methods
misspelling of identifiers and keywords
use of undeclared variables and so on
sometimes, a program may compile successfully and create .class file but
may not run properly. such programs may produce wrong results due to
wrong logic or may terminate due to errors.
java exception handling is managed via 5 keywords: try, catch, throw, throws
and finally (all these are inter related, which means use of one implies the use
of another).
Program statements that you want to monitor for exceptions are contained
within a TRY block. if an exception occurs within the try block, it is thrown. your
code can catch the exception using CATCH and handle it in some manner.
system generated exceptions are automatically thrown by the java run-time
systems. To manually throw an exception, use the keyword THROW. In some
cases, an exception that is thrown out of a method must be specified as such
by a THROWS clause.
Any code that absolutely must be executed upon exiting from a try block is put
in a finally block.
Example program.
we can associate more than one catch clause with a try. each catch
must catch a different type of exception.
Example program
In multiple catch clauses, a catch clause for a superclass will also match
any of its subclasses.
if you want to catch exceptions of both superclass type and subclass type,
then put the subclass first in the catch sequence. if you do not, then the
superclass catch will also catch all derived classes.
if you put the superclass catch first, causes unreachable code. since the
subclass catch clause can never execute. In java, unreachable code
causes a compile-time error.
An exception generated within the inner try block that is not caught by a
catch associated with that try is propagated to the outer try block.
Example program
sometimes you want to define a block of code that will execute when a
try/catch block is left(for example, some actions which may need to
perform before it ends).
A finally block will be executed whenever execution leaves a try/catch
block, no matter what condition causes it.
the finally block is also executed if any code within the try block or any of
its catch clauses return from a method.
ArithmeticException
Exception Description
Arithmetic error, such as divide-by-zero.
20
ArrayIndexOutOfBoundsException Array index is out-of-bounds.
UnsupportedOperationException
Faculty: Mrs.M.Lalitha, Assistant Professor, CSE, GNITS An unsupported operation was encountered.
list of Java Checked Exceptions: 21
Exception Description
ClassNotFoundException Class not found.
in java, exceptions is its ability to handle exceptions that you create which
correspond to errors in your program.
creating an exception is easy just define a subclass of exception.
exception class does not define any methods of its own. so it uses
throwable methods. if we want we can override these methods.
2 commonly used exception constructors are
1) Exception()---> it has no description
2) Exception(String s) --> it specify a description of the exception.
Example program.