Exception Handling in Java Tutorial

In this section, we will learn what the exception handling is and how it works in Java.

What Is Exception in Java?

In short, an interruption in the flow of a running program that derails the program from its normal flow of execution and probably causes the program to crash is called exception.

Types of Error in Java

In two situations, a program will halt (crash):

  • Compile time error
  • Runtime Exception

Compile Time Error in Java

When we use a wrong syntax in a program like forgetting a semicolon `;` or creating a variable that does not follow the rules that apply to the name of that variable or creating a method in a wrong way etc. All of these mistakes are compile time error.

We call them compile time error because they will be appeared at compile time and the compiler will catch them before the program actually starts to run!

Example: compile time error

public class Simple {

    public static void main(String[] args) {
        FuncInterface fc
    }
}

Output:

Error:(6, 25) java: ';' expected

As you can see, we didn’t put the semicolon `;` after the `fc` variable and the compiler saw this when it was compiling the program and so it abrupt by returning an error. This error shows what the problem is and where we need to look into in order to fix it.

Runtime Exception in Java

On the other hand, we have Runtime exceptions.

According to Longman dictionary, exception means something (or someone) that is not included in a general statement or does not follow a rule or pattern.

For example, we say: It’s been cold, but today’s an exception.

The general pattern here was that at least the last few days had a cold temperature, but today didn’t follow that pattern and so we have an exception here.

Exceptions happen in programs as well. For example, we might write a program that accepts text files, reads their content and shows that content on the screen.

The pattern here is clear and as long as we send an uncorrupted file to this program, it will do its job.

One exception that might occur in this program is when we put the address of a file that does not exist! Another exception that might occur is when we put a file with different format! For example, instead of a text file, we send an audio file!

In such a situation, the program stops running and basically will crash! This is the only thing we don’t want to happen to our program.

Exceptions always happen in a program no-matter how powerful that program is written, but fortunately Java provided tools that we can use to handle such exceptions at runtime and stop a program from crashing.

What is Exception Handling in Java?

In short, the process of handling an exception in a program in order to stop that error from crashing a program is called exception handling.

In the table below, you can see the tools for handling exceptions and a short description of their use cases.

In the next couple of sections, we will dive deep into these tools and explain them one by one.

Name

Description

try{//body…}

The `try{}` is a block that we put codes we think might produce exception at runtime. For example, the part of a program that is in charge of opening an external resource (files) might end up throwing an exception. So this is an example of code that can be put in this block.

catch( ){//body…}

After the exception occurred, there should be a set of instructions to run (Think of it as an emergency plan). The `catch(){}` block that comes after the `try` block is the place where we put the exception handling instructions.

Note: these instructions only run if an exception occurred.

finally{//body… }

When an exception occurs in a `try` block, we either handle it in a `catch` block or somehow the exception will crash the program. But even if we failed at handling the exception, we might want a set of instructions to run before the program crashes! The `finally` block is the place where we can put those instructions.

Note: the `finally` block comes after the `try and catch` blocks and so it will be run after those blocks.

throw

Java is smart enough to see exceptions and automatically throw them. But sometimes we want to manually throw an exception. This is where we can use the `throw` keyword and throw an exception.

throws

We use the `throws` keyword at the signature of a method to declare the number of exceptions that the target method might throw. We do this because sometimes we don’t want to handle exceptions in a method and want the caller of that method to be responsible for handling the exceptions.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies