Java Throw and Throws

In this article, we will see the close difference between Java throw and throws which are the concepts of exception handling.

  • Throws keyword is used to declare exceptions that can occur during the execution of a program. The throws keyword will be helpful for propagating exceptions within the decision stack and permits exceptions to not essentially be handled within the strategy that declares these exceptions.
  • On the opposite hand, the throw keyword is employed at intervals in a technique body, or any block of code, and is employed to expressly throw one exception. The throw keyword will be helpful for throwing exceptions supported for conditions within a code block and for throwing custom exceptions.

Java Throws Keyword

For any technique which will throw exceptions, it’s mandatory to use the throws keyword to list the exceptions which will be thrown. The throws keyword provides info regarding the exceptions to the programmer and additionally on the caller of the strategy that throws the exceptions.

The syntax for throws keyword:

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

In the above syntax, we declare more than one exception using the throws keyword.

Code:

Java Code

import java.io.*;
class Main {
  public static void writeToFile() throws IOException {
    BufferedWriter bw = new BufferedWriter(new FileWriter("myFile.txt"));
    bw.write("Test");
    bw.close();
}

public static void main(String[] args) {
try {
        writeToFile();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
}

In the above example, the “writeToFile” method throws an IOException and declares it using the throws keyword to its callers. The “main” method calls the “writeToFile” method and handles the exception within a try-catch block, and prints the exception stack trace to the console.

Java Throw Keyword

Throw keyword is used for single custom exception explicitly or manually,we can throw checked or unchecked exceptions in java.

Syntax for throw in Java:

throw throwableObject;

Example: UnChecked Exception

It reflects some error inside the program logic. In this example, we are explicitly throwing an ArithmeticException.

Code:

Java Code

class Main {
  public static void multiplyByZero() {
    throw new ArithmeticException("Trying to multiplying 0");
  }
  public static void main(String[] args) {
    multiplyByZero();
  }
}

Output:

Exception in thread “main” java.lang.ArithmeticException: Trying to multiplying 0
at Main.multiplyByZero(Main.java:3)
at Main.main(Main.java:7)

Example: Checked Exception

It represent errors outside the control of the program. For example, IOException,FileNotFoundException

Code:

Java Code

private static void checkedExceptionWithThrows() throws FileNotFoundException {
    File file = new File("not_existing_file.txt");
    FileInputStream stream = new FileInputStream(file);
}

Conclusion:

Throw keyword is used within a method (or constructor) whereas Used with method (or constructor) signature.

Throw Can only throw a single exception,but throws can declare multiple exceptions.

Throw Cannot be used to propagate checked exceptions by itself but throws can do it by their own

Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article