Throws in Java

HOME

The throws keyword in Java is used in the signature of method to indicate that this method might throw one of the listed type of exceptions. The caller to these methods has to handle the exception using a try-catch block.

Syntax of throws

return_type method_name() throws ExceptionType1, ExceptionType2 …{  
//method code  
} 

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

Below is an example of throws exception. Here, as you can see IOException is listed as an exception. This exception is handled by a try catch block when findFile() method is called.

public class ThrowsExample {

	public static void findFile() throws IOException {

		File file= new File("C:\\test.txt");
		FileInputStream stream = new FileInputStream(file);

	}

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

Output
java.io.FileNotFoundException: C:\test.txt (The system cannot find the file specified)

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.

In a program, if there is a chance of raising an exception then compiler always warn us about it and compulsorily we should handle that checked exception, Otherwise we will get compile time error saying unreported exception XXX must be caught or declared to be thrown. To prevent this compile time error we can handle the exception in two ways: 

  1. We have caught the exception i.e. we have handled the exception using try/catch block.
  2. We have declared the exception i.e. specified throws keyword with the method.

Case 1 : Handle Exception Using try-catch block

In case we handle the exception, the code will be executed fine whether exception occurs during the program or not.

class Demo {
	void method() throws IOException {
		throw new IOException("IOException Occurred");
	}
}

public class TestThrowsExample2 {

	public static void main(String[] args) {
		try {
			Demo demo = new Demo();
			demo.method();
		} catch (Exception e) {
			System.out.println("Exception handled");
		}

		System.out.println("Continue the program...");
	}
}

Output
Exception handled
Continue the program...

Case 2: We declare the exception, if exception does not occur, the code will be executed fine.

class Test {
	void method() throws IOException {
		System.out.println("No Exception");
	}
}

public class TestThrowsExample3 {

	public static void main(String[] args) {
		try {
			Test test = new Test();
			test.method();
		} catch (Exception e) {
			System.out.println("Exception handled");
		}

		System.out.println("Continue the program...");
	}
}

Output
No Exception
Continue the program...

Case 3 :  We declare the exception and the exception occurs, it will be thrown at runtime because throws does not handle the exception.

class TestDemo {
	void method() throws IOException {
		throw new IOException("IOException Occurred");
	}
}

public class TestThrowsExample4 {

	public static void main(String[] args) throws IOException {
		TestDemo test = new TestDemo();
		test.method();
		System.out.println("Continue the program...");

	}

}

Output
Exception in thread "main" java.io.IOException: IOException Occurred

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s