Writing Custom Exception Handlers in Java Programming
Previous | Home | Next |
whenever we have declare our own Exceptions then we call it as custom exception.
Custom Exception inherit the properties from the Exception class.
in below example if any exception occurs in fetchException method()
then custom exception will be thrown
class CustomException extends Exception
{
String value="This is a example of custom Exception";
public String toString()
{
return "Custom Exception: " + value;
}
CustomException(String v)
{
value = v;
}
}
void fetchException(String value) throws CustomException
{
if(value != ""){
throw new CustomException(value);
}
try {
fetchException("This is a custom Exception");
fetchException("");
} catch (CustomException e) {
System.out.println("Exception: " + e);
}
Previous | Home | Next |