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:
  1. package com.instanceofjava;
  2. class InvalidAgeException extends Exception{  
  3.  InvalidAgeException(String s){ 
  4. super(s);   
  5. }
  6. }  
Program:
TestUsrDefinedException,java:
  1. package com.instanceofjava;
  2. class TestUsrDefinedException{ 
  3.  static void validate(int age)throws InvalidAgeException{  
  4.  if(age<18)  
  5.   throw new InvalidAgeException("Invalid age");
  6.  else  
  7.  System.out.println("welcome to vote");  
  8. }  
  9. public static void main(String args[]){
  10.  try{  
  11.  validate(13);  
  12.  }
  13. catch(Exception m){
  14. System.out.println("Exception occured: "+m);
  15. }
  16. finally{
  17.  System.out.println("This block will be Executed")
  18. }
  19. }
  20. }   


Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu