Check out example codes for "java throw an exception". It will help you in understanding the concepts better.

Code Example 1

public class ThrowException{
  public static void main(String [] args) throws Exception{ 
    //throws Exception line is needed if not using try-catch block
    throw new Exception("Errmessage");

Code Example 2

public static void main(String[] args) {
	Scanner kb = new Scanner(;
    System.out.println("Enter a number");
    try {
    	double nb1 = kb.nextDouble();
        	throw new ArithmeticException();
        else System.out.println( "result : " + Math.sqrt(nb1) );
    } catch (ArithmeticException e) {
        System.out.println("You tried an impossible sqrt");

Code Example 3

/* In this program we are checking the Student age
 * if the student age<12 and weight <40 then our program 
 * should return that the student is not eligible for registration.
public class ThrowExample {
   static void checkEligibilty(int stuage, int stuweight){ 
      if(stuage<12 && stuweight<40) {
         throw new ArithmeticException("Student is not eligible for registration"); 
      else {
         System.out.println("Student Entry is Valid!!"); 

   public static void main(String args[]){ 
     System.out.println("Welcome to the Registration process!!");
     checkEligibilty(10, 39); 
     System.out.println("Have a nice day.."); 

Learn ReactJs, React Native from