public class TestThrow1 {
static void validate(int age) {
if (age < 18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote on TechSarvam");
}
public static void main(String args[]) {
validate(13);
System.out.println("rest of the code...");
}
}
Step-by-Step Explanation of the Program:
This Java program demonstrates how to manually throw an exception in Java, using the keyword throw statement when a certain condition is met. For this example, it checks if a person’s age is less than 18 and causes an ArithmeticException upon such an occurrence by writing a personal message to the console.
Now, let’s break down the code step by step:
1. Method Declaration – validate
static void validate(int age) {
if(age < 18)}
throw new ArithmeticException(“not valid”);
else
System.out.println(“welcome to vote on TechSarvam”);
}
- validate(int age): It is a method that takes an integer age as its argument.
- Condition: In the method, it will check if the age is less than 18.
- If the condition is valid (i.e., age <18), an exception is thrown using throw new ArithmeticException(\”not valid\”).
- If age is 18 or above, then it prints a welcoming message: “welcome to vote on TechSarvam”.
2. Throwing an Exception
throw new ArithmeticException(\”not valid\”);
- The keyword is used to explicitly throw an exception in Java. In this case, it throws an arithmetic exception with the message “not valid”.
- If there’s a catch then the control of program is shifted to that particular catch block, if no catch then the exception will end the normal program flow.
3. main Method
public static void main(String args[]) {
validate(13);
System.out.println(“rest of the code.”);
}
- Calling validate(13): The main method will call the validate() method by passing argument 13 as age = 13.
- Since 13 < 18, the condition in the validate method invokes the throw statement, and an exception, ArithmeticException, is thrown.
- The line System.out.println(\”rest of the code.\”); will not be executed because the exception stops the normal flow of execution before this line is reached.
4. Exception Handling
- There is no try-catch block surrounding the call to the validate() method, so when the exception is thrown, the program exits and the message of the exception (not valid) is printed.
- The exception propagates up and the execution of the program is terminated.
Output:
Because the exception is thrown (age is less than 18), the message “not valid” is printed, and the program terminates without executing any further code.
The output will be as follows:
Exception in thread “main” java.lang.ArithmeticException: not valid
at TestThrow1.validate(TestThrow1.java:4)
at TestThrow1.main(TestThrow1.java:10)
Explanation of Output:
- Exception in thread “main”: This shows that the exception occurred in the main thread of the program.
- java.lang.ArithmeticException: not valid: This is the type of exception (ArithmeticException) and the custom message (not valid) that was thrown.
- at TestThrow1.validate(TestThrow1.java:4): This means the exception happened at line 4 of the validate method inside the TestThrow1 class.
- at TestThrow1.main(TestThrow1.java:10): This suggests that the main method at line 10 had thrown the exception.
Main Points
The throw statement is used to throw the exceptions manually. Upon throwing an exception, the execution of the program is stopped; if it is not resolved, then the program halts.
Since there isn’t any try-catch block in this code, the program will terminate when an exception has occurred and also print the message of the exception.
Conclusion
This program demonstrates the usage of the throw statement to enforce certain conditions and stop execution upon violating the said condition, such as in age validation. If you want to handle the exception and let the program continue execution, you can wrap the method call with a try-catch block to catch and handle the exception.