Mark Balhoff
3
Q:

java throw an exception

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");
  }
}
1
public static void main(String[] args) {
	Scanner kb = new Scanner(System.in);
    System.out.println("Enter a number");
    try {
    	double nb1 = kb.nextDouble();
    	if(nb1<0)
        	throw new ArithmeticException();
        else System.out.println( "result : " + Math.sqrt(nb1) );
    } catch (ArithmeticException e) {
        System.out.println("You tried an impossible sqrt");
    }
}
1
/* 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.."); 
 } 
}
1

New to Communities?

Join the community