INTRODUCTION
Whenever
any error is generated the corresponding exception will be fired. An exception
is thrown when an unexpected abnormal error is occurred. If an exception is
uncaught then the current thread of execution will be terminated, but before
termination, Thread Group is allowed to handle the exception. If
there is no solution against the fired exception, then only information and
details all about the exception can be shown.
Whenever
any exception is thrown, the runtime environment creates an instance of the
corresponding exception and then sends it to the corresponding catch block.
TYPES OF EXCEPTION
All
exceptions are subclasses of the in-built class Throwable. So Throwable
is the topmost class in the exception class hierarchy. There are two
subclasses of Throwable, one is the Exception class and the second is the Error class.
The
Exception class is used for the exceptional conditions that are handled by the
programmer and they should catch it. Runtime Exception is the important
subclass of the Exception class. These types of exception fired are
automatically defined for the programs. The Error is another branch of Throwable; these are the exceptions that
are not expected to be caught under normal conditions of your programs. These
are the errors that occur at run time and are handled by the run time environment.
Stack overflow is an example.
Hierarchy of
Throwable class
DEALING WITH EXCEPTION
Before taking the tour on how to handle the exceptions, take a look at the program without using exception handling.
In the above-given code, there is no syntax error. But a logical error is there, that the valve is
0 (zero), and no number can be divided by zero. This error cannot be checked at
the time of compilation.
But as we run it, an
exception will be fired, and the system-generated message is given below.
The
message shows that the message generated by the JVM, stating that an exception
is fired in the "main" method of type "java.lang.ArithmeticException"
that is for any number divided by zero showing in the message as "/by
zero" and the line number "8" in method "main" of
program "DemoProg.java”. So as the exception fired, the run time
environment caught it and generated the system-supported message.
Handling Exception
Now we study how to
handle the exception. To handle the exception we have to Sandwich the code
between the try block braces, and should catch the exception. The exception
handling block is given below:
try {
// the of statements or code
has to monitor for errors"
}
catch (ExceptionType1
exobj) {
// exception handling
tor ExceptionType1
//the set of actions have
to be taken when an exception has been caught
}
catch (Exception Type2
exObJ) {
// exception handling
for Exception Type2
}
//The set of actions have
to be taken when an exception has been caught
//…
finally {
//The set of actions have
to be executed before the end of the try block either exception fired or not
}
Whatever the code is written in the try block under observation and monitoring, as any exception will be fired, will be thrown, that will be handled by the corresponding related catch block. The catch block will receive the corresponding Exception object. It's the block of code that will execute for the fired exception or the type of exception object that has been received by it. So, the programmer must know how much the exception can be thrown and what type of, accordingly number of corresponding catch statements have to be added with the try block.
Now, in the immediately above given code the expression
'c=a/b" is in the try block, and the Arithmetic Exception is caught. Now see
what will be the output of the code if we run it.
A user-given message Divided by zero" is showing that was given in each block. As the exception was caught the instruction "System.out.println ("Divided by Zero")" was executed in the corresponding catch block. But the major difference in the output is that in the last code output, the value of e did not print, as the exception was caught by the runtime environment, and the program was terminated, but here the point to be noted is that the exception was tired, and handled here so that after executing the catch block of the corresponding statement the rest of the code is also executed, it prints the value of e.
Use of Exception Object
The
Exception class is the superclass of all exception category classes. So, if the exception is unknown, then in the catch block the Exception class object can be
used to receive either type of thrown exception object.
Multiple Catch
There
may be a possibility that more than one exception will be fired in a single try
block of code programmer must know which type of exceptions can be thrown,
and for each and every type of exception, there must be a catch block.
Nested Try Block
In
Java, a nested try block is a structure where one try block is placed inside
another. This allows for more granular exception handling, where specific
exceptions can be caught and processed at different levels within the nested
structure. Each try block can have its own set of catch and finally blocks.
Let's look at an illustrated example to understand the use of nested try blocks:
Use of throw
As an exception occurs, Java run time systems create an instance of the corresponding exception and throw implicitly. However, if there is a need to throw an exception explicitly, then a throw statement is used.
The general statement of throw is shown here:
throw
ThrowableInstance;
The ThrowableInstance must be an object of either type. It
can be of Throwable or any subclass of Throwable. Any non-subclass of Throwable
cannot be used. There are two ways to obtain the Throwable object, one is as a
parameter into a catch clause, or by creating one instance by a new operator. The
subsequent statements will never execute after the throw statement if the
exception is thrown. The searching of the corresponding catch handler will
remain the same as discussed in the nest try block.
If we run this code without passing any
argument or more than two arguments from the command line, then the output is
If we pass exact two arguments as numbers then the output is
Use of throws
This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses. The unchecked exception must be in the throws list if it is thrown by the method. If they are not included then the compile time error will be generated.
This is the
general syntax form of a method declaration that includes a throws clause:
type method_name(parameter_list) throws exception_list
{
//body of method
}
Here, exception_list is a comma-separated list of all the exceptions that can be thrown by the method.
output:Use of finally
When exceptions are thrown, the execution of
the code is not linear, there is an interruption. The subsequent statements will
never execute from where the exception will be fired in the block. But if we want
that, a set of statements must execute either exception throw or not, then the
set of the statements must execute either exception throw or not, and then the set of
statements has to be written or encapsulated in the finally block.
Output
EXCEPTION OBJECTS
Exception class is the superclass of all
subclasses of Exception Category. As we know, the superclass reference variable
can have the object of its subclasses. By this concept, if the exception to be
thrown is unknown or we want to write on one catch handler for all types of
exception, then all throws exception can be received in the Exception object.
Examples are given below :
Output
But the most important concept is that the catch handler that is using the Exception class object for all types of exceptions, must be in the last of a series of multiple catch handlers, otherwise an error message will be generated by the compiler at the compilation time of the program.
Java's built-in
exceptions can handle the most common errors. However, they are not able to handle the application-specific situations. These situations are not matched with predefined
conditions by Java. These are user-defined conditions for her/his application or
module. Java provides the easiest way to define its own exception just by
extends Exception class, and the system treats your class as an exception class. Exception
class does not define any method of its own.
The exception will be thrown on the following three conditions:
- When there is no argument
- When the number of arguments is more than two
- When there is only one argument
As the program received
two arguments then it will print the sum of the passed value.
No comments:
Post a Comment