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

ExceptionHandling 1

Uploaded by

Shivani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ExceptionHandling 1

Uploaded by

Shivani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

EXCEPTION HANDLING

1
Contents

 Introduction
 Exception- Class Hierarchy.
 Types of Exception
 Exception Handling in Java
 Difference between throw and throws
 Difference between final, finally and finalize

2
Introduction

 An exception is an event, which occurs during the execution of a program,


that disrupts the normal flow of the program's instructions.

 An exception can occur for many different reasons, including the following:

– A user has entered invalid data.

– A file that needs to be opened cannot be found.

3
 Any exception which is thrown, must be caught by an exception handler.

 If the programmer hasn't provided one, the exception will be caught by a


catch-all exception handler provided by the system.

 The default exception handler may terminate the application.

 Exceptions can be re-thrown if the exception cannot be handled by the block


which caught the exception

4
Exception -Class Hierarchy

5
Errors are problems that mainly occur due to the
lack of system resources. It cannot be caught or
handled. It indicates a serious problem. It occurs
at run time. These are always unchecked. An
example of errors is OutOfMemoryError,,, etc.
are the subclasses of the Error class.

6
Types of Exception

 Checked exceptions: Exceptions that are checked at compile-time are called


checked exceptions. The classes that directly inherit the
Throwable class except RuntimeException and Error are
known as checked exceptions
 A checked exception is an exception that is typically a user error or a
problem that cannot be foreseen by the programmer. For example, if a file
is to be opened, but the file cannot be found, an exception occurs.
Some common CheckedExceptions in Java:
 FileNotFoundException
 SQLException

7
 Unchecked exceptions: An Unchecked exception is an exception that occurs
at the time of execution, these are also called as Runtime Exceptions. The
classes that inherit the RuntimeException are known as
unchecked exceptions
 For example, if you have declared an array of size 5 in your program, and
trying to call the 6th element of the array then an ArrayIndexOutOfBounds
Exception exception occurs.
Some common Unchecked Exceptions in Java:
 ClassCastException
 IndexOutOfBoundsException

8
Scenario of Exceptions

9
10
Exception Handling in Java

Java has 5 keywords for exception handling:


– try
– catch
– finally
– throw
– throws

 try block: Java try block is used to enclose the code that might throw an
exception. It must be used within the method.
 Java try block must be followed by either catch or finally block.

11
 catch block: Java catch block is used to handle the Exception. It must be
used after the try block only.
 You can use multiple catch block with a single try.

12
Internal working of try-catch block

13
 Multi catch block: If we have to perform different tasks at the occurrence of
different Exceptions, then java multi catch block should be used.

14
 nested try block: Sometimes a situation may arise where a part of a block may
cause one error and the entire block itself may cause another error. In such
cases, exception handlers have to be nested.

15
16
Throws keyword: The Java throws keyword 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.

17
Throw keyword: The Java throw keyword is used to explicitly throw an exception.
The throw keyword is mainly used to throw custom exception.

18
Difference between throw and throws

19
 finally block: Java finally block is used to to put "cleanup" code such as
closing a file, closing connection etc.
 Java finally block is always executed whether exception is handled or not.
 Java finally block must be followed by try or catch block.

20
21
Thank you

22

You might also like