0% found this document useful (0 votes)
3 views

U3 IT JAVA Exception in Java

Exception handling in Java is a powerful mechanism to manage runtime errors and maintain the normal flow of applications. There are two main types of exceptions: checked exceptions, which are checked at compile-time, and unchecked exceptions, which are checked at runtime. Key concepts include the use of try, catch, throw, throws, and finally keywords to handle exceptions effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

U3 IT JAVA Exception in Java

Exception handling in Java is a powerful mechanism to manage runtime errors and maintain the normal flow of applications. There are two main types of exceptions: checked exceptions, which are checked at compile-time, and unchecked exceptions, which are checked at runtime. Key concepts include the use of try, catch, throw, throws, and finally keywords to handle exceptions effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

EXCEPTION IN JAVA

 The Exception Handling in Java is one of the


powerful mechanism to handle the runtime errors so that normal
flow of the application can be maintained.
 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.
 What is Exception Handling

Exception Handling is a mechanism to handle runtime


errors such as ClassNotFoundException, IOException,
SQLException, RemoteException, etc.
EXCEPTION IN JAVA
 Types of Java Exceptions
There are mainly two types of exceptions:
checked and unchecked.
 Checked Exception

 Unchecked Exception

Error:
Here, an error is considered as the
unchecked exception.

 Error is irrecoverable e.g. OutOfMemoryError,

VirtualMachineError, AssertionError etc.


HIERARCHY OF JAVA EXCEPTION CLASSES
EXCEPTION IN JAVA
Checked Exception
 The classes which directly inherit Throwable class

except RuntimeException and Error are known as


checked exceptions e.g. IOException, SQLException
etc. Checked exceptions are checked at compile-time.
Unchecked Exception
 The classes which inherit RuntimeException are

known as unchecked exceptions e.g.


ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time, but they
are checked at runtime.
EXCEPTION IN JAVA
JAVA EXCEPTION KEYWORDS
 try:
To maintain risky code.
 catch:

To maintain handling code.


 throw:

To handover our created exception object


to the JVM manually.
 throws:

To delegate responsibility of exception


handling to the caller method.
 finally:

To maintain cleanup code.


GENERAL FORM OF AN EXCEPTION-HANDLING BLOCK
WITH/WITHOUT TRY/CATCH
JAVA MULTI-CATCH BLOCK

A try block can be followed by one or more catch


blocks. Each catch block must contain a different
exception handler.
NESTED TRY STATEMENTS

 The try statement can be nested. That is, a try statement


can be inside the block of another try.
JAVA FINALLY BLOCK

 Java finally block is a block that is used to execute

important code such as closing connection, stream etc.

 Java finally block is always executed whether

exception is handled or not.

 Java finally block follows try or catch block.


JAVA FINALLY BLOCK
FINAL VS FINALLY VS FINALIZE
JAVA CUSTOM EXCEPTION

 If you are creating your own Exception that is


known as custom exception or user-defined
exception. Java custom exceptions are used
to customize the exception according to user
need.

Syntax:

class MyException extends Exception

}
JAVA CUSTOM EXCEPTION
Methods to print exception information:

You might also like