Exception Handling: What Is An Exception?
Exception Handling: What Is An Exception?
Exceptional handling: The block of code that react to a specific type of exception. If the
exception is for error that the program can recover from, the program can resume executing after
the execution handler has executed .Java provides us facility to create our own exceptions which
are basically derived classes of Exception.
Exception handling is one of the most important features of java programming that allows us to
handle the runtime errors caused by exceptions. In this guide, we will learn what is an exception,
types of it, exception classes and how to handle exceptions in java with examples.
What is an exception?
An Exception is an unwanted event that interrupts the normal flow of the program. When an
exception occurs program execution gets terminated. In such cases we get a system generated error
message. The good thing about exceptions is that they can be handled in Java. By handling the
exceptions we can provide a meaningful message to the user about the issue rather than a system
generated message, which may not be understandable to a user.
Exception Handling
If an exception occurs, which has not been handled by programmer then program execution gets
terminated and a system generated error message is shown to the user. For example look at the
system generated exception below:
An exception generated by the system is given below
This message is not user friendly so a user will not be able to understand what went wrong. In
order to let them know the reason in simple language, we handle exceptions. We handle such
conditions and then prints a user friendly warning message to user, which lets them correct the
error as most of the time exception occurs due to bad data provided by user.
Difference between error and exception
Errors indicate that something severe enough has gone wrong, the application should crash rather
than try to handle the error.
Exceptions are events that occurs in the code. A programmer can handle such conditions and take
necessary corrective actions. Few examples:
There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as
the unchecked exception. According to Oracle, there are three types of exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
Difference between Checked and Unchecked Exceptions
1) Checked Exception
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler
checks them during compilation to see whether the programmer has handled them or not. If these
exceptions are not handled/declared in the program, you will get compilation error. For example,
SQLException, IOException, ClassNotFoundException etc.
2) Unchecked Exception
Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not checked at
compile-time so compiler does not check whether the programmer has handled them or not but
it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. For
example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
3) Error
Keyword Description
try The "try" keyword is used to specify a block where we should place exception code.
The try block must be followed by either catch or finally. It means, we can't use try block alone.
It must be preceded by try block which means we can't use catch block alone.
finally The "finally" block is used to execute the important code of the program.
It doesn't throw an exception. It specifies that there may occur an exception in the method.
try
{
// statement(s) that might cause exception
}
2.catch : Catch block is used to handle the uncertain condition of try block. A try block is always
followed by a catch block, which handles the exception that occurs in associated try block.
catch
{
// statement(s) that handle an exception
// examples, closing a connection, closing
// file, exiting the process after writing
// details to a log file.
}
3.throw: Throw keyword is used to transfer control from try block to catch block.
4.throws: Throws keyword is used for exception handling without try & catch block. It specifies
the exceptions that a method can throw to the caller and does not handle itself.
Throw keyword is used in the method body to throw an exception, while throws is used in method
signature to declare the exceptions that can occur in the statements present in the method.
5.finally: It is executed after catch block. We basically use it to put some common code when there
are multiple catch blocks.
A finally block contains all the crucial statements that must be executed whether exception occurs
or not. The statements present in this block will always execute regardless of whether exception
occurs in try block or not such as closing a connection, stream etc.
1. you provide a web form for users to fill in and submit.but incase there are a lot of
conditions to be handled and the conditions keeps changing periodically,you use
exception handling to simplify the process
2. database connectivity uses exception handling(why???) this is because the reason for
database connectivity failure cannot be predicted and handled as it can be caused by
many variables such as power failure, unreachable server,failure at client front/back end
and so on.
3. internet communication
4. arithmetic exceptions such as division by zero and so on.
5. operating systems use exception handling to resolve deadlocks,recover from crash and so
forth.