Java throw keyword

The throw keyword is used to explicitly throw an exception.

We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exception.

The syntax of java throw keyword is given below.

throw exception;

Let's see the example of throw IOException.

throw new IOException("sorry device error);

java throw keyword example

We want to show an error message if the marks of student are below 75. We need to throw an error message — “Please reappear for exam “.

class StudentExcepeption {

	static void validateMarks(int age){

     if(marks < 75)
  throw new ArithmeticException("Reappear for exam");
   else
   System.out.println("Student is having Distinction");

   }

	public static void main(String args[]) {
		validateMarks(71);
		System.out.println("Remaining code...");

	}

}
				

 Output: Exception in thread main java.lang.ArithmeticException:not valid 
				

Java throws keyword

The Java throws keyword is used to declare an exception.

Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

Syntax of java throws

  1. return_type method_name() throws exception_class_name{
  2. ...
  3. }

Which exception should be declared

Ans) checked exception only, because:

  • unchecked Exception: under your control so correct your code.
  • error: beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or StackOverflowError.

Advantage of Java throws keyword

Now Checked Exception can be propagated (forwarded in call stack).

It provides information to the caller of the method about the exception.

Java throws example

 import java.io.IOException;

class Testthrows {
	void m() throws IOException {
		throw new IOException("device error");// checked exception
	}

	void n() throws IOException {
		m();
	}

	void p() {
		try {
			n();
		} catch (Exception e) {
			System.out.println("exception handled");
		}
	}

	public static void main(String args[]) {
		Testthrows obj = new Testthrows();
		obj.p();
		System.out.println("normal flow...");
	}
}


	 exception handled
  normal flow...
 

Rule: If you are calling a method that declares an exception, you must either caught or declare the exception.


There are two cases:

  • Case1: You caught the exception i.e. handle the exception using try/catch.
  • Case2: You declare the exception i.e. specifying throws with the method.

Difference between throw and throws:

throw keyword throws keyword
1)throw is used to explicitly throw an exception. throws is used to declare an exception.
2)checked exception can not be propagated without throws. checked exception can be propagated with throws.
3)throw is followed by an instance. throws is followed by class.
4)throw is used within the method. throws is used with the method signature.
5)You cannot throw multiple exception You can declare multiple exception e.g.
public void method()throws IOException,SQLException.

Que) Can we rethrow an exception?

Yes by throwing same exception in catch block.