Apart from java we have Existing Exceptions. and we can also create our own Exceptions nothing but User defined Exceptions.
User defined exceptions in java are also known as Custom exceptions.
Program:
InvalidAgeException .java:
Program:
TestUsrDefinedException,java:
InvalidAgeException .java:
- package com.instanceofjava;
- class InvalidAgeException extends Exception{
- InvalidAgeException(String s){
- super(s);
- }
- }
TestUsrDefinedException,java:
- package com.instanceofjava;
- class TestUsrDefinedException{
- static void validate(int age)throws InvalidAgeException{
- if(age<18)
- throw new InvalidAgeException("Invalid age");
- else
- System.out.println("welcome to vote");
- }
- public static void main(String args[]){
- try{
- validate(13);
- }
- catch(Exception m){
- System.out.println("Exception occured: "+m);
- }
- finally{
- System.out.println("This block will be Executed")
- }
- }
- }
No comments