DEV Community

Cover image for Exception Handling with JDK 6 vs JDK 7 and above.
Benjamin Thorpe
Benjamin Thorpe

Posted on • Updated on

Exception Handling with JDK 6 vs JDK 7 and above.

Exception handling is one of the most important concept in any programming language, so sooner or later in your journey you will have to learn it.
This article is not to talk about Exception Handling in general but some changes made in the Java syntax and how to take advantage of that change (called try-with-resources).

The syntax of the normal try-catch-finally block:

try {
    // code to try
} catch(Exception e){
    // code when error occurs
} finally{
    // code to run no matter the outcome
}
Enter fullscreen mode Exit fullscreen mode

The syntax of the try-with-resources:

try (AutoCloseable objects){
    // code to try
} catch(Exception e){
    // code when error occurs
} finally{
    // code to run no matter the outcome
    // the finally block can be optional.
}
Enter fullscreen mode Exit fullscreen mode

Now lets say we want to write to a file.

In JDK 6 and below, we write it as:

File file = new File(test.txt);
FileWriter fw = null;
try {
   fw = new FileWriter(file); 
   fw.write(Hello World in the file!!\n);
   // fw.close(); // close it if everything goes well (optional)
} catch(IOException ioe){
    System.out.println(Error writing to file);
} finally{
    try {
        if(fw != null){
        fw.close();  // we close the file
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

It was a good way and it still is, but we have to always close the file manually. What if we forget to close the file, or forget to include the finally block, then there will be a resource leak. In a small program its will seem ok but in a big program,... :(

In JDK 7 and above, we write it as:

File file = new File(test.txt);
try (FileWriter fw = new FileWriter(file);){
   fw.write(Hello World to the file!! \n);
}catch(IOException ioe){
    System.out.println(Error writing to file);
}finally{
    // do something else
    // the finally block is optional
}
Enter fullscreen mode Exit fullscreen mode

We can even add many more Closeable objects inside the parenthesis, but they must be separated by semicolon.
eg:

try(FileWriter fw = new FileWriter(parameter); 
    PrintWriter pw = new PrintWriter(parameter);){
    ......
// rest of code
Enter fullscreen mode Exit fullscreen mode

Its much easier now, because Java will automatically close the file after the try-catch block.
There are a few limitations to this, the try-with-resources works with:

  • Only Classes that implements the AutoCloseable interface.
  • The Object must be instantiated inside the parenthesis, eg:
FileWriter fw = new FileWriter(parameter);
Enter fullscreen mode Exit fullscreen mode

It is not always that good to use try-with-resources, like making a chat app, because the server needs to be open for connections, so that client can send and retrieve data.

You can use both ways and they will work on any version of Java you are using, it depends on what you feel comfortable using.

Top comments (0)