You are on page 1of 42

Contents

• Introduction
• Types of Errors
• Exceptions
• Syntax of Exception Handling Code
• Multiple Catch Statements
• Using Finally Statement
• Throwing our own exceptions
13.1 Introduction
• It is common to make mistakes while
developing a progam.
• Mistake can lead to error.
• Errors are the wrongs that make the program
go wrong.
• An error may produce an incorrect output or
may terminate the execution of the program
abruptly.
• Errors can even cause systems to crash.
13.2 Types of Errors
• Errors are classified into two types:
– Compile-time errors
– Run-time errors
• Compile time errors are detected and displayed by
the Java compiler.
• It is important to fix these errors in order to compile
and run the program.
• Some of the common compile time errors:
– Missing semicolons
– Missing brackets in classes and methods
13.2 Types of Errors
– Misspelling of identifiers and keywords
– Missing double quotes in strings
– Use of undeclared variables
– Incompatible types in assignments/initialization
– Bad references to objects
– Use of = in place of == operators
13.2 Types of Exception
• Most common run-time errors are:
– Dividing an integer by zero
– Converting invalid string to a number.
– Accessing a character that is out of bounds of a
string.
13.3 Exceptions
• An exception is a condition that is caused by a run-time error in
the program.
• An exception is an abnormal condition that arises in a code
sequence at run time. In other words, an exception is a run-time
error.
• If the exception is not caught and handled properly, the interpreter
will display an error message and the program will be terminated.
• Exception handling is a mechanism of detecting and reporting an
exceptional circumstance:
– Find the problem(Hit the exception)
– Inform that an error has occurred(Throw exception)
– Receive the error information(catch the exception)
– Take corrective actions(Handle the exception)
Error is created in response to catastrophic failures that cannot be
handled by the program.
13.4 Syntax for Exception Handling Code
13.2 Types of Exception
13.2 Types of Exception
• Exceptions in Java can be of two types:
• Checked Exceptions: These exceptions are
explicitly handled in the code itself.
• Unchecked exceptions: Not essentially
handled in the program code, instead the JVM
handles such exceptions.
13.4 Syntax for Exception Handling Code
………
………
try
{
statement; //generates an exception
}
catch(exception-type)
{
statement; //Processes the exception
}
………
………
13.4 Syntax for Exception Handling Code
13.4 Syntax for Exception Handling Code
13.4 Syntax for Exception Handling Code
13.5 Multiple Catch Statements
13.6 Using Finally Statement
• Java supports finally statement that can be used
to handle exception that is not caught by any
previous catch statements.
• Finally block can be used to handle any exception
generated within a try block.
• It can be added immediately after the try block
or after the last catch block. try
{
try ……….
{ }
………. Catch(…)
} {
finally ……………..
{ }
……….. finally
} {
………..
}
13.7 Throwing Our Own Exceptions
There would be times when we would like to
throw our own exceptions. We can do this by
using the keyword throw as follows:
throw new Throwabl’e subclass;
Examples:
throw new ArithmeticException();
throw new NumberFormatException();
• Program below demonstrates the use of a user-defined
subclass of Throwable class. Note that Exception is a subclass
of Throwable and therefore MyException is a subclass of
Throwable class.
• An object of a class that extends Throwable can be thrown
and caught.
//Example of creating own customized
exception
• Output:
Throws
• There could be situations where there is a possibility that a
method might throw certain kinds of exceptions but there
is no exception handling mechanism prevalent within the
method.
• In such a case, it is important that the method caller is
intimated explicitly that certain types of exceptions could
be expected from the called method, and the caller must
get prepared with some catching mechanism to deal with
it.
• The throws clause is used in such a situation. It is specified
immediately after the method declaration statement and
just before the opening brace.
• Output:
Checked Exceptions
• Java forces you to handle these error scenarios in some
manner in your application code.
• They will come immediately into your face, once you start
compiling your program.

• You can definitely ignore them and let them pass to JVM, but
it’s bad habit. Ideally, you must handle these exceptions at
suitable level inside your application so that you can inform
the user about failure and ask him to retry/ come later
• Checked exceptions are subclasses of Exception class.
• Example of checked
exceptions are : IOException, SQLException and so on.
Unchecked Exceptions

• Java also provides UncheckedExceptions, the


occurrences of which are not checked by the
compiler
• They will come into life/occur into your
program, once any buggy code is executed.
• Here are the few unchecked exception classes:
• NullPointerException
• ArrayIndexOutOfBoundsException
• ArithmeticException
• IllegalArgumentException
• NumberFormatException
• Here are the few other Checked Exceptions –
• SQLException
• IOException
• ClassNotFoundException
• InvocationTargetException
• How to resolve the exception?
• There are two ways to avoid exception. We
will see both the ways one by one.
• Method 1: Declare the exception using throws
keyword.
• Method 2: Handle them using try-catch blocks.

You might also like