in , ,

Java throw and throws

Java throw and throws
Java throw and throws

Java throw and throws

Java throw and throws: In this tutorial, we will learn to use the throw and throws keyword for exception handling with the help of examples.

In Java, exceptions can be ordered into two teypes:

• Unchecked Exceptions: They are not checked at order time yet at run-time.For example: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, exceptions under Error class, and so forth

Checked Exceptions: They are checked at compile-time. For instance, IOException, InterruptedException, and so on

Refer to Java Exceptions to learn in detail about checked and unchecked exceptions.

For the most part, we don’t need to handle unchecked exceptions. This is on the grounds that unchecked exceptions happen because of programming mistakes. What’s more, it is a decent practice to correct them instead of handling them.

This tutorial will now focus on how to handle checked exceptions using throw and throws.


Java throws keyword

We use the throws keyword in the method declaration to declare the type of exceptions that might occur within it.

Its syntax is:

accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 … {
  // code
}

As you can see from the above syntax, we can use throws to declare multiple exceptions.


Example 1: Java throws Keyword

import java.io.*;
class Main {
  public static void findFile() throws IOException {
    // code that may produce IOException
    File newFile=new File("test.txt");
    FileInputStream stream=new FileInputStream(newFile);
  }

  public static void main(String[] args) {
    try{
      findFile();
    } catch(IOException e){
      System.out.println(e);
    }
  }
}

Output

java.io.FileNotFoundException: test.txt (No such file or directory)

When we run this program, if the file test.txt does not exist, FileInputStream throws a FileNotFoundException which extends the IOException class.

If a method does not handle exceptions, the type of exceptions that may occur within it must be specified in the throws clause so that methods further up in the call stack can handle them or specify them using throws keyword themselves.

The findFile() method specifies that an IOException can be thrown. The main() method calls this method and handles the exception if it is thrown.


Throwing multiple exceptions

Here’s how we can throw multiple exceptions using the throws keyword.

import java.io.*;
class Main {
  public static void findFile() throws NullPointerException, IOException, InvalidClassException {
    
    // code that may produce NullPointerException
    … … … 

    // code that may produce IOException
    … … … 

    // code that may produce InvalidClassException 
    … … … 
  }

  public static void main(String[] args) {
    try{
      findFile();
    } catch(IOException e1){
      System.out.println(e1.getMessage());
    } catch(InvalidClassException e2){
      System.out.println(e2.getMessage());
    }
  }
}

Here, the findFile() method specifies that it can throw NullPointerException, IOException, and InvalidClassException in its throws clause.

Note that we have not taken care of the NullPointerException. This is on the grounds that it is an unchecked exception. It isn’t important to determine it in the throws condition and handle it.


throws keyword Vs. try…catch…finally

There may be a few techniques that can cause exceptions. Writing try…catch for every strategy will be dull and code becomes long and less-readable.

throws is additionally helpful when you have checked exception (an exception that must be handle) that you don’t want to catch in your current method.


Java throw keyword

The throw keyword is used to explicitly throw a single exception.

When an exception is thrown, the flow of program execution transfers from the try block to the catch block. We use the throw keyword within a method.

Its syntax is:

throw throwableObject;

A throwable object is an instance of class Throwable or subclass of the Throwable class.


Example 2: Java throw keyword

class Main {
  public static void divideByZero() {
    throw new ArithmeticException("Trying to divide by 0");
  }

  public static void main(String[] args) {
    divideByZero();
  }
}

Output

Exception in thread "main" java.lang.ArithmeticException: Trying to divide by 0
    at Main.divideByZero(Main.java:3)
    at Main.main(Main.java:7)
exit status 1

In this example, we are explicitly throwing an ArithmeticException.

Note: ArithmeticException is an unchecked exception. It’s usually not necessary to handle unchecked exceptions.


Example 3: Throwing a checked exception

import java.io.*;
class Main {
  public static void findFile() throws IOException {
    throw new IOException("File not found");
  }

  public static void main(String[] args) {
    try {
      findFile();
      System.out.println("Rest of code in try block");
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  }
}

Output

File not found

The findFile() method throws an IOException with the message we passed to its constructor.

Note that since it is a checked exception, we must specify it in the throws clause.

The methods that call this findFile() method need to either handle this exception or specify it using throws keyword themselves.

We have handled this exception in the main() method. The flow of program execution transfers from the try block to catch block when an exception is thrown. So, the rest of the code in the try block is skipped and statements in the catch block are executed.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Exception Handling

Java Exception Handling

Java catch Multiple Exceptions

Java catch Multiple Exceptions