Explain the exception handling mechanism of Java?
Exception class inherits from the Throwable class in java. Java has a try-catch mechanism for handling exceptions without them being generated as errors.
public class Exception_Handling {
String gender;
Exception_Handling(String s){
gender=s;
}
void Check_Gender(String s) throws GenderException{
if (gender!="Male" || gender!="Female")
throw new GenderException("Gender Invalid");
else
System.out.println("Gender valid");
}
public static void main(String args[]){
Exception_Handling n=new Exception_Handling("None");
try{
n.Check_Gender("Female");
}catch (Exception e){
System.out.println("Exception : "+e);
}
}
}
class GenderException extends Exception{
GenderException(String s){
super(s);
}
}
🔗Reference: stackoverflow.com
🔗Source: Java Interview Questions and Answers