0% found this document useful (0 votes)
34 views5 pages

Oops 3

The document discusses exception handling in Java programming. It describes what exceptions are, common reasons they occur, and different types of exceptions. It then explains how exceptions are handled in Java using try, catch, throw, throws, and finally keywords and provides examples of their usage.

Uploaded by

Anindya Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

Oops 3

The document discusses exception handling in Java programming. It describes what exceptions are, common reasons they occur, and different types of exceptions. It then explains how exceptions are handled in Java using try, catch, throw, throws, and finally keywords and provides examples of their usage.

Uploaded by

Anindya Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exception Handling in Java

An exception in java programming is an abnormal situation that is raised during the


program execution. In simple words, an exception is a problem that arises at the time of
program execution.

Reasons for Exception Occurrence

• When we try to open a file that does not exist may lead to an exception.
• When the user enters invalid input data, it may lead to an exception.
• When a network connection has lost during the program execution may lead to
an exception.
• When we try to access the memory beyond the allocated range may lead to an
exception.

Types of Exception

• Checked Exception - An exception that is checked by the compiler at the time of


compilation is called a checked exception.
• 🔔 In the exception class hierarchy, the checked exception classes are the direct
children of the Exception class.
• Unchecked Exception - An exception that can not be caught by the compiler but
occurrs at the time of program execution is called an unchecked exception.
• 🔔 In the exception class hierarchy, the unchecked exception classes are the
children of RuntimeException class, which is a child class of Exception class.

How exceptions handled in Java?


In java, the exception handling mechanism uses five keywords
namely try, catch, finally, throw, and throws.

try and catch in Java


In java, the try and catch, both are the keywords used for exception handling.
The keyword try is used to define a block of code that will be tests the occurence of an
exception. The keyword catch is used to define a block of code that handles the
exception occured in the respective try block.
The uncaught exceptions are the exceptions that are not caught by the compiler but
automatically caught and handled by the Java built-in exception handler.
Both try and catch are used as a pair. Every try block must have one or more catch
blocks.

Multiple catch clauses


🔔 The try block generates only one exception at a time, and at a time only one catch
block is executed.
🔔 When there are multiple catch blocks, the order of catch blocks must be from the
most specific exception handler to most general.
🔔 The catch block with Exception class handler must be defined at the last.

Nested try statements


The java allows to write a try statement inside another try statement. A try block within
another try block is known as nested try block.
🔔 In case of nested try blocks, if an exception occured in the inner try block and it's
catch blocks are unable to handle it then it transfers the control to the outer try's catch
block to handle it.
throw keyword in Java
The throw keyword is used to throw an exception instance explicitly from a try block to
corresponding catch block. That means it is used to transfer the control from try block to
corresponding catch block.
🔔 Using throw keyword only object of Throwable class or its sub classes can be
thrown.
🔔 Using throw keyword only one exception can be thrown.
🔔 The throw keyword must followed by an throwable instance.

throws keyword in Java


The throws keyword specifies the exceptions that a method can throw to the default
handler and does not handle itself. That means when we need a method to throw an
exception automatically, we use throws keyword followed by method declaration
🔔 When a method throws an exception, we must put the calling statement of method
in try-catch block.
finally keyword in Java
The finally keyword used to define a block that must be executed irrespective of
exception occurence.
The basic purpose of finally keyword is to cleanup resources allocated by try block,
such as closing file, closing database connection, etc.
🔔 Only one finally block is allowed for each try block.
🔔 Use of finally block is optional.

You might also like