Whenever you are writing a code for an application then you
have to handle the semantic violations that may occur during the execution of
application.
Exception is a condition which arises due to violation of
semantic rules during the execution of an application.
When such violation of semantic rules occurs during the
execution then application will be terminated abnormally which may cause other
problems. To avoid such conditions like abnormal termination of application and
to report the details of error or violation of semantic rules we use exception
in Java.
The semantic violation is classified into two classes as:
i.
Error: Errors are those semantic
violations of rules which user do not wish to handle and it can be avoided but
when error occurs recovery of application is not possible. Error class is
present in java.lang.Throwable package.
ii.
Exception: Exception class is subclass
of java.lang.Throwable present in java.lang package. Exceptions are those
semantic violations of rules which can be handled and recovery from such
semantic violations of rules is possible.
Moreover exceptions can be classified into following two
classes:
a) Checked
Exception/Compile time Exception:
Checked exceptions are those exceptions which compiler does
not attempt to handle. In this case compiler will try to find out the presence
of the exception handler. Coders are expected to provide the exception handling
mechanism for checked exception types. Compiler will raise an error if it does
not found exception handler that it expect to be provided by the coder.
If a compiler does not find handler for the checked type
exception then it will generate the error in following format:
UnhandledExcpetion
b) Unchecked
Exception/Compile time Exception:
Unchecked exceptions are those
exception which can be handled by the JVM and compiler will not check for the
presence of exception handler. Such exceptions comes under the java.lang.Runtime which is a subclass of java.lang.Object.
JVM will create one object internally for each exception
raised. When compiler founds and exception in code then following series of
tasks will be performed:
i.
First of all JVM will check whether the exception
handler which can handle the current exception is present in current stack or
not. JVM will decide the exception as handled if:
a) Type of
catch block defined can hold the reference of object created for that exception
type.
b) Or if
object created for exception is subclass of exception type specified in catch
block.
ii.
If compiler finds an exception handler that can
handler countered exception in its stack then exception is handled itself.
iii.
If no exception is found in the current scope then
control will be transferred to the JVM. Then if it is unchecked exception then
it will be handled by JVM and if it is checked exception for which user defined
exception handler is required a compile time error will be generated.
You can write more than one catch block to handle the
required exception. In this case you must follow the hierarchy of catch block
and you must handle the exceptions from child to parent.
A ‘try’ block may have multiple exception handlers but if
there are checked exceptions then in each block exception handler must be present.
For unchecked types of exceptions it is not compulsory to have exception
handler in each block.
If an exception is raised in try block then statement
immediately following that statement will not be executed.
Exception
handlers and blocks in Java:
i.
‘try’ block: ‘try’ block is used to encapsulate the statements
which are expected to raise the exception. You can have multiple statements in
‘try’ block but once an exception is raised for one statement then all other
statements which are next in execution line will not executed and control is
transferred to the JVM. Then JVM will start to check whether the required
exception handler is present in current scope or not.
If a statement is not included in a ‘try’ block and if
it raises the exception then JVM will try to resolve the exception by using the
exception handler in current stack, if possible, otherwise compile time error
will be generated.
ii.
‘catch’ block: ‘catch’ blocks are used to provide an
action/implementation that must be executed if exception is found. ‘catch’ will
be executed whenever an exception is found and it contains at least one
‘throwable’ type which is assignment compatible with exception object created
by JVM corresponding to the exception raised.
For each exception there will be only one matching
‘catch’ block that will be executed. If you want to provide common
implementation for multiple exceptions and if they have inheritance
relationship then one ‘catch’ block super type of all the exceptions can be
provided. In this case if exceptions does not have any inheritance relationship
then multiple exception types, separated by pipe (|) can be provided.
Whenever you are providing multiple ‘catch’ blocks then
inheritance relationship between exception types must be maintained i.e. catch
block with subtype should appear at top and catch block should appear at
bottom.
iii.
‘finally’ block: ‘finally’ block is contains the implementation that
must be executed irrespective of the exception is raised or not. In other words
‘finally’ block is provided to ensure the implementation whether the exception
is raised or not. Normally ‘finally’ is
used to release the resources which are held by implementation inside the ‘try’
block.
If an exception is raised then first implementation
from ‘catch’ block will be executed and then implementation from ‘finally’
block will be executed. But finally block will not execute if:
Ø JVM is
terminated. (System.exit(0)).
Ø If current
thread which is executing the try block will be terminated.
User defined exceptions/Custom exceptions: You can
create your own exception by extending the ‘Exception’ or ‘RuntimeException’.
If you are required to create unchecked exception then you
must create a custom class and that custom class must extend ‘Exception’ or any
other subclass of ‘Exception’ but you can extend your custom class by extending
‘RuntimeException’, in this case.
If you are required to create a checked exception then you
must create a custom class and it must extend ‘RuntimeException’ or its
subclass. In this case you can not extend
your custom class with ‘Exception’.
If you are required to display the message when an exception
is raised then that message must be passed from custom exception class
constructor to super class constructor.
Keyword ‘throw’ can be used to raise or generate the custom
exception or predefined exception.
Only ‘throws’ or subclass of throwable are allowed with throw
statement.
Syntax:
throw;
Ex:
throw new RuntimeException(“RuntimeException”);
throw new Exception(“Exception”);
throw new InvalidUserNameException(“Invalidusername”);
If checked exception object is created with ‘throw’ keyword
then either handler must be provided in current context or exception should be
propagated.
[modifier]
[throws]{
//Statement(s);
}
0 comments:
Post a Comment