I've created a program to ask the user to their name and age, and I've made a custom exception but it won't initiate any tips on how to make it better or how to get the exception to initiate correctly is appreciated but take note I'm still learning java.
User-Defined Exception: (Age Exception)
package ageName;
@SuppressWarnings("serial")
public class AgeExcpt extends Exception {
public AgeExcpt(){
super("The Age you've entered is not valid");
}
}
Main Program:
package ageName;
import java.util.Scanner;
@SuppressWarnings("resource")
public class Project1 {
public static void main(String[]args){
Scanner age = new Scanner (System.in);
System.out.println("Enter your age");
int a = age.nextInt();
System.out.println("Enter you name");
String b = age.next();
try{
if(a >125 && a<0){
throw new AgeExcpt();
}
}
catch(AgeExcpt ex){
System.out.println("You entered an invalid number" + a);
}
finally{
System.out.println("Your age is " + a);
System.out.println("Your name is " + b);
}
}
}
The expression a > 125 && a < 0
is universally false.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加