Home » Java » throw and throws keyword in java

throw and throws keyword in 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. We will see custom exceptions later.

Only object of Throwable class or its sub classes can be thrown. Program execution stops on encountering throw statement, and the closest catch statement is checked for matching type of exception.

Syntax :

throw ThrowableInstance

Let's see the example to throw IOException.

throw new IOException System Error

java throw keyword example :

In this example, we have created the validateAge() method that takes integer value as a parameter. If the age is less than 18, we are throwing the ArithmeticException otherwise print a message you are adult, see the example below.


package com.jwt.java;

public class ExceptionExample {
	static void validateAge(int age) {
		if (age < 18)
			throw new ArithmeticException("error : You are not adult");
		else
			System.out.println("You are adult");
	}

	public static void main(String args[]) {
		validateAge(16);
		System.out.println("other statement ...");
	}
}

Output :

Exception in thread "main" java.lang.ArithmeticException: error : You are not adult
	at com.jwt.java.ExceptionExample.validateAge(ExceptionExample.java:6)
	at com.jwt.java.ExceptionExample.main(ExceptionExample.java:12)

throws keyword :

throws keyword is used to specify the method level exception.Generally when you are implementing a method, code inside that method may throw exceptions. You have two alternatives to deal with this exception.

1. Handle the exception inside the method.
2. Propagate Exception to the caller of the method.

If you want to handle the exception in the same method then put code inside the method in try and catch block. If you want to propagate this exception to the caller of the method then use throws keyword.

In short this is the keyword which gives an indication to the calling function to keep the called function under try and catch blocks.Throws keyword is mainly used for handling checked exception.If any unchecked exception is occurring in the code such as NullPointerException, it is programmers fault that he has not perform null check before the code being used.

Syntax of java throws

return_type methodname throws exception class name

Java throws example :

Let's see the example of java throws clause which describes that checked exceptions can be propagated by throws keyword.

CALLED FUNCTION


package com.jwt.java;

public class ExceptionExample {
	public void div(String s1, String s2) throws ArithmeticException,
			NumberFormatException {
		int n1 = Integer.parseInt(s1);
		int n2 = Integer.parseInt(s2);
		int n3 = n1 / n2;
		System.out.println("DIVISOIN = " + n3);
	}
}    

CALLING FUNCTION


package com.jwt.java;

public class Test {
	public static void main(String[] args) {
		try {
			String s1 = args[0];
			String s2 = args[1];
			ExceptionExample exceptionExample = new ExceptionExample();
			exceptionExample.div(s1, s2);
		} catch (ArithmeticException ae) {
			System.out.println("DONT ENTER ZERO FOR DENOMINATOR");
		} catch (NumberFormatException nfe) {
			System.out.println("PASS INTEGER VALUES ONLY");
		} catch (ArrayIndexOutOfBoundsException Aibe) {
			System.out.println("PASS VALUES FROM COMMAND PROMPT");
		}
	}
}

If you are running the Test class using command prompt and passsing the first value as 10 and second as 0 you will get following output.

DONT ENTER ZERO FOR DENOMINATOR

If you are running the Test class using command prompt and passsing the first value as "a" and second as "b" you will get following output.

PASS INTEGER VALUES ONLY

If you are running the Test class using command prompt without any values you will get following output.

PASS VALUES FROM COMMAND PROMPT

Previous Next Article

comments powered by Disqus