Hi there,

I am Rohan!

""""✰I am always blown away to see thought leaders like Rohan with ambition, having a positive impact on the world!"✰ Michael Walmsley, CEO and founder of CodeAvengers.
Join Rohan Chaubey on

Exception Handling In Java!

In our previous posts we covered about the Characteristics of Java, How to install Java, Classes, Objects, White Spaces, Data Types, Variables, Array Of Objects, Method overriding, Constructor Overloading, Scanner class, Inheritance, Interfaces, Abstract Classes, Access Specifiers and Packages... Click here to read the previous Post! 

What is an Exception? 
  1. Exceptions in real life are rare.
  2. Exceptions are usually used to denote something unusual that does not conform to the standard rules.
  3. In programming, exceptions are events that arise due to the occurrence of unexpected behavior in certain statements, disrupting the normal execution of a program.
Causes of Exception?
Exceptions occur due to number of reasons.
For example,
  1. Trying to access the 11th element of an array when the array contains of only 10 element (ArrayIndexOutOfBoundsException)
  2. Division by zero (ArithmeticException
  3. Accessing a file which is not present (FileNotFoundException)
  4. Failure of I/O operations (IOException)
  5. Illegal usage of null. (NullPointerException).
Exception classes : 

Top class in exception hierarchy is Throwable.
This class has two siblings: Error and Exception. 
All the classes representing exceptional conditions are subclasses of the Exception class.

What happens when an exception occurs? 

Runtime environment identifies the type of Exception and throws the object of it. 

If the method does not employ any exception handling mechanism the exception is passed to the caller method, and so on. 

The default handler prints the name of the exception along with an explanatory message followed by stack trace at the time the exception was thrown and the program is terminated.  
If no exception handling mechanism is employed in any of the Call Stack methods the runtime environment passes the exception object to the default exception handler available with itself.  

Types  Of Exception : 
Types of exceptions in java
Exception Handling Techniques : 
  1. try..catch : try/catch block can be placed within any method that you feel can throw exceptions.
    All the statements to be tried for exceptions are put in a try block catch block is used to catch any exception raised from the try block. If exception occurs in any statement in the try block control immediately passes to the corresponding catch block.

  2. throw : used to explicitly throw an exception. useful when we want to throw a user-defined exception. The syntax for throw keyword is as follows: throw new ThrowableInstance;
    For example : throw new NullPointerException();

  3. throws : is added to the method signature to let the caller know about what exception the called method can throw. responsibility of the caller to either handle the exception (using try…catch mechanism) or it can also pass the exception (by specifying throws clause in its method declaration). If all the methods in a program pass the exception to their callers (including main( )), then ultimately the exception passes to the default exception handler. 

  4.  finally : finally block is executed in all circumstances if the exception occurs or
    it is normal return (using return keyword) from methods. mandatory to execute statements like related to release of resources, etc. can be put in a finally block. The syntax of the finally keyword is as follows:
    try {……}
    catch(Throwable e){……}
    finally {……..}
Now that you know what exception handling is, start implementing them in your programs... Click Here For Programs On Exception Handling!

Happy Learning. 

ABOUTRohan Chaubey

Rohan Chaubey is the founder of “Be Rohanlicious” – a blog that teaches you to be smart with social media, blogging and programming.

Rohan is on a mission to help all the enthusiastic souls like you, who want to “make it to the next level” and stand out of the crowd.

His e-book “Make People Want You” is releasing soon. Allow him to inject awesomeness in your life by signing up to his list here.