Loading

throw and throws keywords in Java exception handling

  • throw and throws are the different keywords in Java, and both keywords have their own functionality in Java.

throw keyword in Java

Key points:

  • The throw keyword is used to throw an object of throwable type in the code.
  • throw keyword must be used inside the method body or constructor body.
  • we can throw only one throwable type of object in the code using the throw keyword.

Syntax:

throw new ArithmeticException();
ArithmeticException e1=new ArithmeticException();                                                                                                                   throw e1;
throw new String();
  • Error because String is not a Throwable type.

Example:

package com.quipoin;
public class ThrowKeyword {
	static void divide(int n1,int n2) {
		if(n2==0) {
			throw new ArithmeticException("Denominator cant be zero");
		}
		else {
			System.out.println("dividing "+n1+" by "+n2);
			int res=0;
			res=n1/n2;
			System.out.println("Result is "+res);
		}
	}
	public static void main(String[] args) {
		try {
			divide(12, 0);
		}
		catch (ArithmeticException e) {
			System.out.println("Printing exception details");	
			e.printStackTrace();
		}
	}
}

Output:

Printing exception details
java.lang.ArithmeticException: Denominator cant be zero
	at com.quipoin.ThrowKeyword.divide(ThrowKeyword.java:6)
	at com.quipoin.ThrowKeyword.main(ThrowKeyword.java:17)

Key points:

  • Exceptions are always thrown at runtime.
  • whenever an unchecked exception category object is thrown, the runtime system looks for the handler in the current method, if not looks for the handler in the caller method.
  • If the execution stack if no handler, the entire stack will collapse and the data will be lost is known as stack unwinding.
  • Whenever the throw statement throws a checked exception category object, the JVM looks for the handler in the current method body if not found compiler throws an unhandled error.

throws keyword in Java

  • throws keyword is used to propagate an exception to its caller function.
  • It must be declared in the method signature(declaration).
  • By using the throws keyword we can propagate multiple exceptions to the caller method.

Syntax

void test() throws SQLException;

The handler for the checked exception is defined in the caller function body, the exception propagation is done by using the throws declaration statement  

package com.quipoin;

import java.io.File;
import java.io.IOException;

public class ThrowsKeyword {
	void create(String path) throws IOException{     //called function body
		File f1=new File(path);
		f1.createNewFile();
	}
	public static void main(String[] args) {         //caller function body
		System.out.println("Main method started");
		ThrowsKeyword t1=new ThrowsKeyword();
		try {
			t1.create("c:\\keshava\\JECM84\\book.txt");
		} catch (IOException e) {
			System.out.println("Handling exception due to:"+e.getMessage());
			e.printStackTrace();
			System.out.println("Main method ended");
		}
	}
}

Output:

Main method started
Handling exception due to:The system cannot find the path specified
Main method ended
java.io.IOException: The system cannot find the path specified
	at java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.io.File.createNewFile(Unknown Source)
	at com.quipoin.ThrowsKeyword.create(ThrowsKeyword.java:9)
	at com.quipoin.ThrowsKeyword.main(ThrowsKeyword.java:15)

 Propagating multiple exceptions from a function body:

package com.quipoin;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public class FileCreation {
	void create(String path) throws IOException,SQLException{
		File f1=new File(path);
		f1.createNewFile();
	}
	public static void main(String[] args) {
		System.out.println("Main method started");
		FileCreation fc=new FileCreation();
		try {
			fc.create("c:\\keshava\\JECM84\\book.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:

Main method started
java.io.IOException: The system cannot find the path specified
	at java.io.WinNTFileSystem.createFileExclusively(Native Method)
	at java.io.File.createNewFile(Unknown Source)
	at com.quipoin.FileCreation.create(FileCreation.java:9)
	at com.quipoin.FileCreation.main(FileCreation.java:15)