A rejoint le : 12 avr. 2022
À propos

Exception Handling

An unwanted, unexpected event that disturbs the normal flow of a program is called Exception. For example File not found exception, sleep exception.


The main objective of Exception handling is Graceful termination of the program. Exception handling avoids abnormal termination of program. It is not executed in case of an error.

Exception handling doesn’t mean repairing an exception, we have to define an alternate way to continue the rest of the program.

• Runtime Exceptions – Arithmetic, null pointer, array index out of bound, number format exception.

• Input/output Exception – File not found.

Types of Exceptions :

• Checked Exceptions-

According to homework answers websites the Exceptions which are checked by compiler for smooth execution of a program at run time are called checked exceptions. For example File not found exception.

• Unchecked Exception-

The Exceptions which are not checked by compiler during execution of a program at run time are called unchecked exceptions.

For example arithmetic exception.

• Errors- They are non recoverable. They occur due to lack of system resources. For example Stack overflow error. Exception Handling Keywords: • try:

According to studydaddy the lines of code which can generate exception are placed in the try block. There can be other code inside try block, before and after the risky lines of code. The risky lines are in try block therefore the exception is skipped.

• catch: Generally there is a at least one catch block immediately after a try block which must specify what type of exception it will catch.

There can be multiple catch blocks each of them marked with exception class. The catch block(s) must handle all checked exceptions that the try block is known to throw unless you want to throw that exception back to the method.

• throw:

The throw statement causes termination of the normal flow of control of the C++ code and prevents the execution of the subsequent statements. The throw statement conveys the control to the nearest catch block handling the type of exception object throws. If there is no such catch block, the program terminates.

Declaration of throw-

throw exception;

• finally block-

This block is executed even if the exception occurs or not. If there is an exception which causes the catch block to execute then finally will block will execute after catch block.

More resources:

String in Java

Java Tutorial: Abstract Class

Tokens in Java Java Tutorial: Data Types Java Tutorial