最近捕获的Numberformat异常下面的所有Numberformat异常都是自己执行的 我的程序得到两个学生的名字和三个主题标记并计算学生的平均值。在中间,如果Mark不是整数和其他两个用户定义的异常,我必须捕获No.MultFrimeExtExchange。 无论何时捕获numberFormatexception,下面的其他numberFormatexception都会被自己捕获 import java.util.*; class ude1 extends Exception { public ude1() { System.out.println("User defind exception 1 thrown"); } public String toString() { return "NegativeValueException"; } } class ude2 extends Exception { public ude2() { System.out.println("User defind exception 2 thrown"); } public String toString() { return "ValueOutofBouundException"; } } class excep6 { /* String stud_name; int mark1,mark2,mark3; excep6(String name,int a,int b,int c) { stud_name=name; mark1=a; mark2=b; mark3=c; } public void calculator() { float avg=0; avg=(mark1+mark2+mark3)/3; System.out.println("The average of "+stud_name+" is "+avg); } */ public static void main(String []args) { Scanner in = new Scanner(System.in); int a=0,b=0,c=0,l=2; String std=""; while(l>0) { try{ System.out.println("enter student name"); std=in.next(); System.out.println("enter mark1"); if(in.hasNextInt()) a=in.nextInt(); else throw new NumberFormatException(); if(a<0) { throw new ude1(); } if(a>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());a=0; } catch(ude2 u2) { System.out.println(u2.toString());a=0; } catch(NumberFormatException e) { System.out.println("NumberFormat Exception");a=0; } System.out.println("enter mark2"); try{ if(in.hasNextInt()) b=in.nextInt(); else throw new NumberFormatException(); if(b<0) { throw new ude1(); } if(b>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());b=0; } catch(ude2 u2) { System.out.println(u2.toString());b=0; } catch(NumberFormatException e) { System.out.println("NumberFormatException Exception");b=0; } System.out.println("enter mark3"); try{ if(in.hasNextInt()) c=in.nextInt(); else throw new NumberFormatException(); if(c<0) { throw new ude1(); } if(c>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());c=0; } catch(ude2 u2) { System.out.println(u2.toString());c=0; } catch(NumberFormatException e) { System.out.println("NumberFormatException Exception");c=0; } System.out.println("The average of student "+std+" is "+(a+b+c)/3); l--; } } }

最近捕获的Numberformat异常下面的所有Numberformat异常都是自己执行的 我的程序得到两个学生的名字和三个主题标记并计算学生的平均值。在中间,如果Mark不是整数和其他两个用户定义的异常,我必须捕获No.MultFrimeExtExchange。 无论何时捕获numberFormatexception,下面的其他numberFormatexception都会被自己捕获 import java.util.*; class ude1 extends Exception { public ude1() { System.out.println("User defind exception 1 thrown"); } public String toString() { return "NegativeValueException"; } } class ude2 extends Exception { public ude2() { System.out.println("User defind exception 2 thrown"); } public String toString() { return "ValueOutofBouundException"; } } class excep6 { /* String stud_name; int mark1,mark2,mark3; excep6(String name,int a,int b,int c) { stud_name=name; mark1=a; mark2=b; mark3=c; } public void calculator() { float avg=0; avg=(mark1+mark2+mark3)/3; System.out.println("The average of "+stud_name+" is "+avg); } */ public static void main(String []args) { Scanner in = new Scanner(System.in); int a=0,b=0,c=0,l=2; String std=""; while(l>0) { try{ System.out.println("enter student name"); std=in.next(); System.out.println("enter mark1"); if(in.hasNextInt()) a=in.nextInt(); else throw new NumberFormatException(); if(a<0) { throw new ude1(); } if(a>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());a=0; } catch(ude2 u2) { System.out.println(u2.toString());a=0; } catch(NumberFormatException e) { System.out.println("NumberFormat Exception");a=0; } System.out.println("enter mark2"); try{ if(in.hasNextInt()) b=in.nextInt(); else throw new NumberFormatException(); if(b<0) { throw new ude1(); } if(b>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());b=0; } catch(ude2 u2) { System.out.println(u2.toString());b=0; } catch(NumberFormatException e) { System.out.println("NumberFormatException Exception");b=0; } System.out.println("enter mark3"); try{ if(in.hasNextInt()) c=in.nextInt(); else throw new NumberFormatException(); if(c<0) { throw new ude1(); } if(c>100) { throw new ude2(); } } catch(ude1 u1) { System.out.println(u1.toString());c=0; } catch(ude2 u2) { System.out.println(u2.toString());c=0; } catch(NumberFormatException e) { System.out.println("NumberFormatException Exception");c=0; } System.out.println("The average of student "+std+" is "+(a+b+c)/3); l--; } } },java,exception,Java,Exception,而不是 enter the name sat enter mark1 i NumberFormat exception enter mark2 NumberFormat exception enter mark3 NumberFormat exception enter the name _ 如果调用hasNextInt()并返回false,然后再次调用,则保证再次返回false hasNextInt()不会跳过非整数的标记。它留在扫描仪中,以便您可以尝试将其作为其他内容读取 在大多数情况下

而不是

enter the name 
sat
enter mark1
i
NumberFormat exception
enter mark2
NumberFormat exception
enter mark3
NumberFormat exception
enter the name
_

如果调用
hasNextInt()
并返回
false
,然后再次调用,则保证再次返回
false

hasNextInt()
不会跳过非整数的标记。它留在扫描仪中,以便您可以尝试将其作为其他内容读取

在大多数情况下,从意外输入中恢复的正确方法(例如,当需要整数时为非整数)是调用
nextLine()
并放弃结果。
nextLine()
调用将使用下一行结束之前的所有字符


您还应注意以下评论:

  • 在类名中遵循样式约定,以及
  • 自定义异常的不适当使用

不要这样编写代码。两个用户定义的异常及其抛出和捕获都是完全不必要的。您检测到这些情况:立即处理。您可以将其长度减少到不到其长度的四分之一,并使其更易于理解。方法名和变量名是用camelCase编写的。
enter the name 
sat
enter mark1
i
NumberFormat exception
enter mark2
NumberFormat exception
enter mark3
NumberFormat exception
enter the name
_