Exception Handling

from Head First Java chapter 11

In software development, we can not control everything. Things will may be wrong. Rather than bugs, there will be some risks at runtime that we can not sure whether it will run or be stuck at run time. So this chapter is talking about handling the exception.

An exception is an event that happens unexpectedly and interrupts the flow of work.

Exception handling is the mechanism for handling them and giving a flow to run successfully.

As the compiler does not care about these exceptions and does not know to handle them, When you are coding, if you can identify the particular method that may be risky at run time, you should give instructions to handle the exception itself.

The try/catch block is used to handle the exception.

Try block contains our code that may have some risky things, the catch block contains how you are going to handle this exception. So that If an exception is thrown by the try statement, the catch clause will catch the exception.

The type of exception that you are trying to catch must be given as arguments of the catch.

The below image illustrates the basic structure of the try and catch.

the basic block of try and catch

And, we have to pay attention to which method throws an exception and which method is going to catch the exception.

When you call a method that throws an exception, you have to be aware to catch declare the exception

void readFile() throws Exception {

throw new Exception();


throws: used to declare an exception.

throw : used to throw an exception

The flow of work of try/catch

If the try block runs successfully, then the catch block will not be executed.

if the try block fails, the rest of the code in the try block will not be executed and the catch block will catch the exception.

Type of exception.

Type of exceptions.

There are mainly two types of exception

  1. Checked Exceptions
  2. Unchecked Exception /Runtime Exceptions

Unchecked Exception /Runtime Exceptions

The unchecked exception will not be checked by the compiler. It will be created due to the logic of the code. This exception can happen anywhere (in any method) so, you do not have to throw, catch or declare and these Exceptions are a subclass of Runtime exceptions.



classCast Exception


Checked Exceptions

All Exceptions that are not subclasses of Runtime Exception are known as checked exceptions. It will be checked by the compiler. It will be created due to the fails of conditions at runtime.


trying to read a file that does not exist


Whether an exception is thrown or not, the code in the finally block will be executed always. The finally block is used to execute clean up code like closing connections, closing files, or freeing up threads. At the same, the finally block is optional in try/catch and finally block can not exist without the try/catch block.

Some more about Exception Handling

  1. Throwable is a Superclass of Exception
  2. A method can throw multiple Exceptions. Handling in catch must be in order or based on which exceptions are thrown by try block.
  3. The order must be from subtype to supertype. otherwise, it will show a compile error.
  4. Catch block can be polymorphic so that, declaring supertype extension in catch allows throwing any subtype extension.
  5. Able to catch the subtype exception by supertype exception.
  6. As the Exception to be supertype, it can catch all the types of exceptions.
  7. We can declare exceptions using the throws keyword instead of the try/catch block.

Associate Software Engineer at Virtusa