Explain Exception Handling In Java With Program And Examples, Types Of Exception In Java Program, How To Handle The Java exception
In This Topic & Blog Having Any Query Then Post your Comment and Suggestion Below

Exception-Handling Fundamentals
A Java exception is an object that describes an exceptional (that is, error) condition that has
occurred in a piece of code. When an exceptional condition arises, an object representing
that exception is created and thrown in the method that caused the error. That method may
choose to handle the exception itself, or pass it on. Either way, at some point, the exception
is caught and processed. Exceptions can be generated by the Java run-time system, or they
can be manually generated by your code. Exceptions thrown by Java relate to fundamental
errors that violate the rules of the Java language or the constraints of the Java execution
environment. Manually generated exceptions are typically used to report some error condition
to the caller of a method.
Java exception handling is managed via five keywords:
try catch throw throws
,
,
,
, and
finally
. Briefly, here is how they work. 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 this exception (using
catch
)
and handle it in some rational manner.
System-generated exceptions are automatically thrown by the Java run-time system. To
manually throw an exception, use the keyword
throw
. Any exception that is thrown out of
a method must be specified as such by a
throws
clause. Any code that absolutely must be
executed after a
try
block completes is put in a
finally
block.
This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
205
206
Part I:
The Java Language
catch (ExceptionType1 exOb ) {
// exception handler for ExceptionType1
}
catch ( ExceptionType2 exOb ) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}
Here, ExceptionType is the type of exception that has occurred. The remainder of this chapter
describes how to apply this framework.
Exception Types
All exception types are subclasses of the built-in class
Throwable
. Thus,
Throwable
is at the
top of the exception class hierarchy. Immediately below
Throwable
are two subclasses that
partition exceptions into two distinct branches. One branch is headed by
Exception
.
This class
is used for exceptional conditions that user programs should catch. This is also the class that
you will subclass to create your own custom exception types. There is an important subclass
of
Exception
, called
RuntimeException
.
Exceptions of this type are automatically defined for
the programs that you write and include things such as division by zero and invalid array
indexing.
The other branch is topped by
Error
, which defines exceptions that are not expected to
be caught under normal circumstances by your program. Exceptions of type
Error
are used
by the Java run-time system to indicate errors having to do with the run-time environment,
itself. Stack overflow is an example of such an error. This chapter will not be dealing with
exceptions of type
Error
, because these are typically created in response to catastrophic failures
that cannot usually be handled by your program.
Labels: Java - J2SE