What you should know about Java exceptions

For beginners, learning exception handling is important. In this tutorial, we will go through Java exception and cover important things about it.

Java exception is one of the most important concepts of Java programming. If you're a beginner, you should get a basic understanding of how Java exception works and how you can utilize it in your programming.

To get a clear picture, let's understand what an exception means in the Java programming language.

Java Exception is a simple mechanism for handling runtime errors. If we try to understand "exception" in general, it relates to errors that take place while executing a program. Exception handling is also used in C# programming language. So, if you get an idea of how exception handling works in Java, you can easily use it in other programming languages as well.

For beginners, learning exception handling is important. In this tutorial, we will go through Java exception and cover important things about it. Even many experienced Java developers don't properly use Java exception and suffer from inconsistencies throughout their code.

You should learn basic Java programming before proceeding with this tutorial. It will help you get the basics of Java exception handling. You are encouraged to use a good Java IDE so you don't waste time on trivial things such as indentation, code completion, etc.

The concept of exception handling

At its core, a program is a set of instructions. To run properly, it should not have any logical, runtime, or syntax errors. Whenever a program encounters any runtime error, an exception is raised. The exception object is then thrown by the program, which is handled by the exception handler. Since everything in Java is an object, that means exceptions created by programs are also objects. These objects contain information about the type of exception, as well as additional data that needs to be transmitted.

So, what does it look like? Let's take a look at an example code:

#FileNotFoundException

try {
  nfile = new File("abc.txt");
  nfile.readLine;
  nfile.write("another item for the list");
  nfile.close();
} catch (FileNotFoundException fnfe) {
  system.out.write("File not found, Please make sure that the file is present in the location or you are entering the correct file name");
}

The above is a try-catch code that tries to create a new file. If the creation fails or the file is not present at the current location, the code in the try should throw an exception object. The FileNotFoundException handles the exception object. The coder can either print a statement or perform an action that handles the situation correctly.

Some examples of when exceptions can happen are:

  • Dividing by zero

  • Accessing Array element with an out of bounds index.

  • Passing an incorrect argument to a method.

Checked and unchecked exceptions

There are two type of exceptions: checked and unchecked. Let's go through them.

Checked exception is an exception that needs to be handled by the programmer. These types of exceptions are made mandatory to encourage developers to stay ahead and provide an alternative to common issues. For example, if a file is not present and is being accessed, checked exception should be used. API usage also benefits heavily from checked exception as developers can control different conditions.

Unchecked Exception requires no work from the programmer. The compiler takes care of the issue. For example, nullpointer exception, divide by zero exception, etc. are examples of unchecked exceptions. Runtime exception is also an unchecked exception.

Learning about parent class: Throwable

Throwable is the parent class of all the exceptions. Throwable has two children: Exception and errors. We have already discussed exception. So, what are errors for? Are exception and errors the same thing?

Errors are program conditions that are not handled by programmers. For example, JVM errors can never be handled. Garbage collector errors are also not handled by exception. Exceptions, on the other hand, are handled by either the programmer or the compiler.

Exception classes Hierarchy

 Image source: beginnersbook.com

Try-Catch block

Try-Catch block handles exceptions. If you want a piece of code to be handled correctly, you need to put it in the try block. The catch block will take care of exceptions thrown by the try block. So, the workflow is simple, if an exception occurs in the try block, it throws an exception to the catch block and is handled accordingly. Let's see an example.

try {

int a = 5/b;

}

Catch (ArithmeticException e)

{

//code

}

You can also use Multiple Catch block if needed. Multiple Catch blocks function similar to simple Try-Catch block, however, offers the ability to handle more than one exception.

try {

//code

}

catch (Exception1 e){

//more code to handle exception1

}

catch (Exception2 e1){

//more code to handle exception2

}

Finally block

The finally block is used to ensure that the code runs despite the exception. It is used to ensure that the code executes without worrying if an exception is raised or not. The Try-Catch-Finally code looks like the example below.

try {

//code

}

catch (Exception1 e){

//more code to handle exception1

}

finally {

//this code will always gets executed.

}

Conclusion

Java Exceptions are great way of handling exceptions. It ensures program integrity in any condition. Programmers should always try to use exceptions as it makes their code more reliable. You can get started with Java programming by going through a list of resources here. You may also want to follow some broadcasts to get a better grasp on Java programming.

If you found the article useful, don't forget to comment below and let us know.

Related:

Copyright © 2016 IDG Communications, Inc.