In Java we have already defined exception classes such as ArithmeticException, NullPointerException, ArrayIndexOutOfBounds exception etc. These exceptions are set to trigger on different-2 conditions. For example when we divide a number by zero, this triggers ArithmeticException, when we try to access the array element out of its bounds then we get ArrayIndexOutOfBoundsException.
We can define our own set of conditions or rules and throw an exception explicitly using throw keyword. For example, we can throw ArithmeticException when we divide number by 5, or any other numbers, what we need to do is just set the condition and throw any exception using throw keyword. Throw keyword can also be used for throwing custom exceptions, I have covered that in a separate tutorial, see Custom Exceptions in Java.
Syntax of throw keyword:
throw new exception_class("error message");
For example:
throw new ArithmeticException("dividing a number by 5 is not allowed in this program");
Example of throw keyword
Lets say we have a requirement where we we need to only register the students when their age is less than 12 and weight is less than 40, if any of the condition is not met then the user should get an ArithmeticException with the warning message “Student is not eligible for registration”. We have implemented the logic by placing the code in the method that checks student eligibility if the entered student age and weight doesn’t met the criteria then we throw the exception using throw keyword.
/* In this program we are checking the Student age * if the student age<12 and weight <40 then our program * should return that the student is not eligible for registration. */ public class ThrowExample { static void checkEligibilty(int stuage, int stuweight){ if(stuage<12 && stuweight<40) { throw new ArithmeticException("Student is not eligible for registration"); } else { System.out.println("Student Entry is Valid!!"); } } public static void main(String args[]){ System.out.println("Welcome to the Registration process!!"); checkEligibilty(10, 39); System.out.println("Have a nice day.."); } }
Output:
Welcome to the Registration process!!Exception in thread "main" java.lang.ArithmeticException: Student is not eligible for registration at beginnersbook.com.ThrowExample.checkEligibilty(ThrowExample.java:9) at beginnersbook.com.ThrowExample.main(ThrowExample.java:18)
In the above example we have throw an unchecked exception, same way we can throw unchecked and user-defined exception as well.
For more examples on throw keyword, refer this: Throw keyword examples.
priyanka duggirala says
you said that user defined exceptions use throw clause and java defined use throws. but cannot find difference in examples you explained. can you explain it please.
reshma says
Can anybody explain the use of PrintStackTrace plzzz???
Arjun says
Now I came to know that how We can throw our own custom Exception. I have seen this in our project codebase but never understood but now reading this document, I understand and now I understand my codebase too.
Thanks buddy.
very good work.
really appreciated.