Java Tutorial Series - Handling Exceptions

Exceptions in Java is a topic which can help you deal with the real time problems faced in your project. What if you are getting an exception in stack trace and you simply want your scripts to move forward, in short you can handle expected exceptions in your scrips. In this article we will be learning about exceptions in java.

Handling Exceptions in Java

There are 2 different ways to handle exception in Java -
  1. Adding Try catch block
  2. Add throws declaration
1. Add Throws Declaration -

public class App {

      public static void main(String[] Args) throws FileNotFoundException {
          File file = new File("text.txt");
          FileReader fr = new FileReader(file);
}
}

//prints FileNotFoundException in stack trace

If you will run above code, you will get FileNotFoundException in Stack trace. This will hinder your execution. To overcome this limitation, let's look at Implementation of Try-Catch block.

2. Implementing Try-Catch block -

public class App {

      public static void main(String[] Args) {
          File file = new File("text.txt");
try {        
FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Finished...");              //will still run even if exception is thrown
}
}

Adding multiple throws declaration - 

Multiple throws declaration is also possible through Java. Let's have a look at its syntax below -

public class Test {
          public void Run() throws IOException, ParseException {

throw new ParseException("Error in command list",2);
}
}

Multiple catch block in try catch block - 

Let's have a look at a method which can have multiple catch blocks.

public class App {

      public static void main(String[] Args) {
          File file = new File("text.txt");
try {      
throw ParseException
}
catch(IOException e) 
{
e.printStackTrace();
}
catch(ParseException e) {
e.printStackTrace();
}
}
}

In this case, flow will go into 1 catch block, not in both the blocks. There is one catch point however, we should be catching the parent class first and then we don't need to use child class exception. For example, FileNotFoundException is child of IOException only, we could directly catch IOException and no need to catch FileNotFoundException in another catch block.

Points to remember -
  • Throwable is the parent class of all the exceptions.
  • Finally block can be used after catch block, finally block will always be executed even if exceptions are thrown.
  • In Java 6, if we are using try catch block, we need to close the try block with the reference variable used for FileOutPutStream, if not closed a compile-time exception will be thrown. However, this limitation was overcome in Java 7 features where try-catch block is enclosed by () which doesn't allow to use close at the end of catch block.

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD