Age Exception error in Java

MrGamma195

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);
    }   
}
}
Jared

The expression a > 125 && a < 0 is universally false.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Java exception error causing code to stop suddenly

分類Dev

java error message null pointer exception

分類Dev

java error: Exception in thread "main" java.lang.NoClassDefFoundError

分類Dev

Java Constructors - Why is age returning as zero?

分類Dev

LocalDate exception error

分類Dev

MATLAB function from Java causes Runtime Error (EXCEPTION_ACCESS_VIOLATION)

分類Dev

Flutter: Exception in thread "main" java.util.zip.ZipException: error in opening zip file

分類Dev

Conversion to Dalvik Unable to execute dex:Java heap space and Unhandled event loop exception error in creating map activity

分類Dev

Is throwing an Exception expensive in Java?

分類Dev

Exception Handling Java

分類Dev

Java NullPointer Exception in doInBackground

分類Dev

Exception questions Java

分類Dev

java deserialize exception

分類Dev

Exception in java thread

分類Dev

Java NIO Exception

分類Dev

Java NIO Exception

分類Dev

Null Pointer Exception and getMapAsync Error

分類Dev

Uncaught Error/Exception Handling in Swift

分類Dev

Compiler error when throwing exception

分類Dev

Android Null point exception error

分類Dev

exception Error in parcing text file

分類Dev

I have a error Exception in thread "main" java.lang.NullPointerException when I connect SQL Server and Check User in table

分類Dev

Not able to connect to Oracle DB getting error as java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

分類Dev

Java in scanner, input mismatch exception

分類Dev

Java RMI Class Cast Exception

分類Dev

Java strange class cast exception

分類Dev

Java Hibernate invalid mapping exception

分類Dev

MySql - Java - Class Not Found Exception

分類Dev

Cause an exception on purpose(Android with java)

Related 関連記事

  1. 1

    Java exception error causing code to stop suddenly

  2. 2

    java error message null pointer exception

  3. 3

    java error: Exception in thread "main" java.lang.NoClassDefFoundError

  4. 4

    Java Constructors - Why is age returning as zero?

  5. 5

    LocalDate exception error

  6. 6

    MATLAB function from Java causes Runtime Error (EXCEPTION_ACCESS_VIOLATION)

  7. 7

    Flutter: Exception in thread "main" java.util.zip.ZipException: error in opening zip file

  8. 8

    Conversion to Dalvik Unable to execute dex:Java heap space and Unhandled event loop exception error in creating map activity

  9. 9

    Is throwing an Exception expensive in Java?

  10. 10

    Exception Handling Java

  11. 11

    Java NullPointer Exception in doInBackground

  12. 12

    Exception questions Java

  13. 13

    java deserialize exception

  14. 14

    Exception in java thread

  15. 15

    Java NIO Exception

  16. 16

    Java NIO Exception

  17. 17

    Null Pointer Exception and getMapAsync Error

  18. 18

    Uncaught Error/Exception Handling in Swift

  19. 19

    Compiler error when throwing exception

  20. 20

    Android Null point exception error

  21. 21

    exception Error in parcing text file

  22. 22

    I have a error Exception in thread "main" java.lang.NullPointerException when I connect SQL Server and Check User in table

  23. 23

    Not able to connect to Oracle DB getting error as java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

  24. 24

    Java in scanner, input mismatch exception

  25. 25

    Java RMI Class Cast Exception

  26. 26

    Java strange class cast exception

  27. 27

    Java Hibernate invalid mapping exception

  28. 28

    MySql - Java - Class Not Found Exception

  29. 29

    Cause an exception on purpose(Android with java)

ホットタグ

アーカイブ