Userdefined Exception

class MyEx extends Exception {
	String getMsg() {
		return "Age is not valid";
	}
}
public class UserDefinedException {
	void checkAge(int age) throws MyEx {
		if (age < 16) {
			throw new MyEx();
		}
		System.out.println("Age is correct");
	}
	public static void main(String[] args) {
		UserDefinedException obj = new UserDefinedException();
		try {
			obj.checkAge(40);
		} catch (Exception e) {
		}
	}
}

Download

Exception Example in Java

Exception Example
Exception Propogation
User Defined Exception


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code