how to raise an exception in java code example
Example 1: python raise exception
# this raises a "NameError"
>>> raise NameError('HiThere')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: HiThere
Example 2: java how to throw exception
public class ThrowException{
public static void main(String [] args) throws Exception{
throw new Exception("Errmessage");
}
}
Example 3: how do you handle exceptions in java
I use try & catch blocks to handle any exceptions in my code.
I am familiar with major checked and unchecked exceptions and
handle it accordingly to make my code execution smooth
Example 4: throwing exceptions java
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..");
}
}