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 will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu