3. Introduction
Exceptions in Java are any abnormal, unexpected events or extra-ordinary
conditions that may occur at runtime.
Java Exception Handling is used to handle error conditions in a program
systemically by taking the necessary action.
The java.lang.Throwable class and all its subclasses are collectively the
exception classes.
5. What Happens when an Exception
occurs in a Java Program
When an exception occurs, the method from which the exception
originated, creates a special object known as exception object and hands
it off to the Runtime System.
The exception object contains information about the exception, and the
state of the program when the exception occurred.
Creating an exception object and handing it to the runtime system is called
throwing an exception.
You can manually throw an exception by identifying a scenario you
considered an exception, but in many cases, it is the Java Virtual
Machine(JVM) throwing exceptions manually
7. try catch Clause
If an exception occurs at the particular statement of try block,
the rest of the block code will not execute and code will fall
through to the catch statement.
Syntax of try-catch:
Unlike other statements such as if and else,the enclosing
braces {} sets are required in this case.
8. try catch finally clause
The finally clause purpose is to clean up the resources.It will always get executed whether
an exception occurs in the try clause or not.
You can declare a try-finally block also and you can also declare try-catch-finally together
also.
What you cannot do is just declaring a try clause without a catch,or finally clause.It will
result in compiler error.
Declaring try-finally Declaring try-catch-finally
9. More on try-catch finally
You can put a nested try-catch in outer try block or outer catch block or
outer finally block.
10. throw clause
A program can explicitly throw an exception using the throw keyword besides the
implicit exception thrown.
Syntax:
11. throws clause
throws clause is used to declare an exception. It gives an information to
the programmer that there may occur an exception so it is better for the
programmer to provide the exception handling code so that normal flow
can be maintained.
Syntax:
12. Unchecked Exceptions
Unchecked Exceptions are those exceptions in which the compiler does
not force you to write code to check for the exception or specify the
exception.
In other words, Unchecked Exceptions are those classes which inherit
RuntimeException.
Eg: RuntimeException, ArithmeticException,
ArrayIndexOutOfBoundsException etc.
13. Unchecked Exceptions in Work
Output:
In this example,the program
abruptly ends as the exception is
not handled
Output
In this example,the program continues
after the exception has been caught
and handled by the catch clause so the
program will continue
14. Checked Exceptions
A Checked Exception is a special designation, for a group of
exceptions that the compiler forces compliance of ‘The Catch
or Specify Requirement’.
Catch or Specify Requirement: It states that code that might
throw certain exceptions, must be enclosed by either of the
following:
A try statement that catches the exception. The try must provide an
appropriate handler for the exception.
A method that specifies that it can throw Exception. The method must
provide a throws clause that lists the exception or an appropriate
exception type.
15. Checked Exceptions In Work
Output Output
In this example, the program abruptly
ends as the checked exception is not
handled.
In this example,the program continues
after the checked exception has been
caught and handled by the catch clause
so the program will continue.
16. Table to explain which Exceptions are
Checked or which are Unchecked