Java throw exception


The "throw" keyword is used to invoke an exception explicitly.

 

The throw keyword helps to throw checked or uncheck exception explicitly. It is commonly used to throw a user-defined exception.

 

Syntax:

 throw new exception;

Example

 public class Throwexception{
    public static void main(String args[]){  
        try{  
            int a=5;  
            if(18 > a){
				throw new IllegalAccessException("Not Eligible");
            }
            else{
            	System.out.println("successfull");
            }
        }
        catch(IllegalAccessException e){
            System.out.println(e);
        } 
        finally{
            System.out.println("Always execute");  
        }
  	}  
} 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.