Java Made Clear: Difference between throws and throw


For a novice, both throws and throw keywords are very confusing. "throws" and "throw" works differently in exception handling.
  1. "throws" Keyword is used in two ways
    1. To claim the exception
    2. As an alternative for try-catch
  2. "throw" Keyword is used to throw the exception object

Let us go into details.

I. throws Keyword

throws keyword is useful for both language Designer and Programmer in different ways.

a) Claiming the Exception

A language Designer designs a method and would like to inform the Programmer about the possible problems that may occur while using the method. There by, Programmer can take care of the problems while using the method. Let us see some API method signatures.

public FileInputStream(String filename) throws FileNotFoundException

Programmer uses the above FileInputStream constructor to open a file in read mode passed as parameter. For example,

FileInputStream fis = new FileInputStream(“abc.txt”);

The file abc.txt is opened in read mode. Programmer continues writing the code with some file operations. He executes the program and found the file abc.txt did not exist at all. Now all the code written becomes waste. So, better advice the Programmer to check the file whether exist or not at the beginning itself before writing all laborious code. When informed, atleast he can write an alternative file to open. The advice is given by the Designer with throws keyword in the method signature itself. The exception FileNotFoundException informs the Programmer that the file may not be found used in the code. This is known as claiming the exception.

The Designer claims with throws keyword that there is a problem which should be taken care of. The problem or risk involved is the file being used may not found.

Another example of throws:

Observe the Thread class sleep() method signature.

public static native void sleep(long milliseconds) throws InterruptedException

The sleep() is used by the Programmer to inactivate the running thread for the period of time passed as parameter in milliseconds. Say, example,

Thread.sleep(1000);

The running thread is made to be inactive for 1000 milliseconds. Infact, the inactivation is done by the OS and not by Java. Java is only an adviser to the OS. It is left to the OS to make inactive exactly 1000 milliseconds which sometimes may not be honored due to Thread Scheduler overloading. So there is every possibility that the sleep() method may not work properly. This should be informed to the Programmer. How? The Designer claims the exception by throwing InterruptedException and is written in the method signature itself.

With this knowledge, now define what is throws?

throws is the way the Designer claims the exception. If once claimed by the Designer with throws keyword, anyone who uses the method should handle the exception. That is, anyone who use sleep() method should handle the InterruptedException else the program does not compile. These exceptions that appear in the method signature are known as checked exceptions. Checked exceptions should be handled in the code. Designers say that checked exceptions cause very serious problems in the code if not handled properly.

b) Other way of using throws Keyword

throws can be used as a substitute for try-catch block but not robust and is advised to use at learning stage or in rough code development.

See the following code:

public void display()
{
  FileInputStream fis = new FileInputStream("abc.txt");
}

The above code does not compile as FileInputStream constructor throws a checked exception FileNotFoundException and is not handled in the method display(). The compiler raises "unreported exception" error. See the modified code.

public void display()
{
  try
  {
    FileInputStream fis = new FileInputStream("abc.txt");
  }
  catch(FileNotFoundException e)
  {
    System.err.println("File does not exist sir. " + e);
  }
}

The above code is compiled successfully. Writing always try-catch is tedious and Java designers felt not required at Java learning stage. They give an alternative style. See.

public void display() throws FileNotFoundException
{
  FileInputStream fis = new FileInputStream("abc.txt");
}

This style is not advised as the execution does not proceed if abc.txt file does not exist. It is just to escape try-catch of 7 statements. But in realtime, nobody uses.

II. throw Keyword

throw keyword is used by the Programmer to throw an exception object to the JVM explicitly. By throwing the object, the Programmer can display a warning message and at the same time terminate the execution. see the following code.

if(balance < withdraw)
{
   ArithmeticException e = new ArithmeticException("Insufficient balance in your account");
   throw e;
}

The JVM, if the balance is less than the withdraw amount, it displays the message and terminates the program.

Pass your comments of how far this tutorial on throws and throw is understandable to you.

ArithmeticException e = new ArithmeticException("Insufficient balance in your account");
throw e;

The above two statements can be simplified with one using anonymous object.

throw new ArithmeticException("Insufficient balance in your account");

Finally to say, throw is used to throw the exception to the system.

Gist of the above discussion:

  1. throws is used in two ways a) claiming exception and b) an alternative for try-catch but not advised.
  2. throw is used to throw an exception object to JVM by the Programmer.

2 thoughts on “Java Made Clear: Difference between throws and throw”

  1. Balasubramanian

    Dear Sir,

    Could you please write a note on struts1.1 and Struts 2.x?
    What is the advantage of migrating from 1.2 to 2.0
    Thanks
    Balu

Leave a Comment

Your email address will not be published.