Java Exception class
We can write our custom Exceptions using the Exception class in java.
public class MyException extends Exception {
// Overridden methods
}
The Exception class has the following important methods:
1) Strings toString() executed when sout (e) is ran
2) Void printStackTrace() - prints Stack trace
3) String getMessage() - prints the exception message
Example
package com.company;
import java.util.Scanner;
class MyException extends Exception{
@Override
public String toString() {
return "I am toString()";
}
@Override
public String getMessage() {
return "I am getMessage()";
}
}
class MaxAgeException extends Exception{
@Override
public String toString() {
return "Age cannot be greater than 125";
}
@Override
public String getMessage() {
return "Make sure that the value of age entered is correct";
}
}
public class exception_class {
public static void main(String[] args) {
int a;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
if (a<9){
try{
// throw new MyException();
throw new ArithmeticException("This is an exception");
}
catch (Exception e){
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
System.out.println("Finished");
}
System.out.println("Yes Finished");
}
}
}