Day 5: Exception Handling in Java


First of all lets understand why we need exception handling in Java.

See there are many chances that you will get run time error while executing you code and your program terminates abnormally, to avoid these situation java provide us a mechanism which is known as exception handling.

What is exception :- An Exception can be anything which interrupts the normal flow of the program.

In any program exception can occur at run time as well as at compile time.
  • Exception comes at run time, known as run time exception. also known as Unchecked Exception.
  • Exception comes at compile time, known as compile time exception, also known as checked exception.
  Let take and example for compile time exceptions.


As you can see, we are getting an exception over here saying that there might be a chance that file will not found so compiler force us to write the code in try catch block or declare the method using throws keyword.

It means that all exceptions which are reflecting while compilation known as checked exception.

Now let take another example to under what is meant by run time error.


As you can see in the above example we tried to divide a number via zero  and while compilation we did not get to see any exception however when we run the program we get to see that program throws an error message saying that "Exception in thread "main" java.lang.ArithmeticException: / by zero".

So these type of exception which comes after running the program are known as run time exceptions.

Now let see how we can handle checked and unchecked exceptions in java.

There are 5 keywords used in java exception handling.
  • try
  • catch
  • finally
  • throw
  • throws
Note:- Java try block must be followed either by catch or by finally.


Now lets start with try and catch block first.

let me take an example to show you how we can handle this with try and catch block.



now see the below attached screen print after using try and catch block, remember Always remember that a catch block must always be associated with a try bloc, it can't be used on its own.





now lets understand how catch block actually works.


Declaring an instance of type Exception ensures that any type of exception can be trapped using object e, therefor when the type of execution being thrown is not known at design time,the class Exception can be used to trap the error.


and get message is a function of exception class which will provide the message.


NOTE:- We can have multiple catch blocks with one try block.


Now lets understand the use of Finally keyword.


finally block is something which will always be executed whether we get the exception or not.



let take the example.






now lets discussed about the other two keywords i.e. throw and throws.



Throw keyword is always used to throw the exception explicitly.


lets take an example



so i have explicitly created a exception and throw the same.

now let have a look on throws keyword.

look throws keyword can be used instead of try and catch.

however each time when we use the method in some other method then in that method too we need to throws the exception.


lets take an example






now lets handle this exception using throws in main as well.










Post a Comment