将 InputMismatchException 转换为用户定义的异常
Convert InputMismatchException into user-defined exception
我希望我的代码将 InputMismatchException
的名称更改为 NotANumberException
。
这是我的代码,如果我输入非数字字符,它就会出错。我应该如何解决这个问题?
主要Class:
import java.util.*;
public class Grade {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = input.nextInt();
}
catch(NotANumberException e){
System.out.println(e.notgetMessage());
}
}
}
第二个class:
import java.util.*;
public class NotANumberException extends InputMismatchException{
public String notgetMessage(){
return "You did not input a number. Please try again!";
}
}
您无法在代码中捕获 InputMismatchException 子类型的异常。
此外,您不能更改 java.util Scanner.nextInt
方法以抛出自定义异常 class,因为它是 Java.
的实用程序库
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()
您的作业的可能解决方案是
在您的第一个 class which returns int
中创建一个方法
public class Grade {
//New method
public int getIntegerInput() throws NotANumberException {
Scanner input = new Scanner(System.in);
try {
return input.nextInt();
} catch( InputMismatchException e) {
throw new NotANumberException();
}
}
public static void main(String args[]){
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = getIntegerInput();
}
catch(NotANumberException e){
System.out.println(e.notgetMessage());
}
}
}
PS:正如你所说的这是一项作业,你尝试花精力学习Java类型系统和扩展异常class并添加throws签名。这只是帮助您完成作业的原型。
您必须捕获扫描程序 class 实际抛出的异常,然后对其进行处理,例如创建并抛出您的自定义异常。
catch(InputMismatchException e){
throw new NotANumberException(e);
}
包 com.geek.test;
导入java.util.InputMismatchException;
导入java.util.Scanner;
public class 测试 4 {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = input.nextInt();
}
catch(InputMismatchException e){
System.out.println("You did not input a number. Please try again!");
}
}
}
我希望我的代码将 InputMismatchException
的名称更改为 NotANumberException
。
这是我的代码,如果我输入非数字字符,它就会出错。我应该如何解决这个问题?
主要Class:
import java.util.*;
public class Grade {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = input.nextInt();
}
catch(NotANumberException e){
System.out.println(e.notgetMessage());
}
}
}
第二个class:
import java.util.*;
public class NotANumberException extends InputMismatchException{
public String notgetMessage(){
return "You did not input a number. Please try again!";
}
}
您无法在代码中捕获 InputMismatchException 子类型的异常。
此外,您不能更改 java.util Scanner.nextInt
方法以抛出自定义异常 class,因为它是 Java.
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()
您的作业的可能解决方案是
在您的第一个 class which returns int
中创建一个方法public class Grade {
//New method
public int getIntegerInput() throws NotANumberException {
Scanner input = new Scanner(System.in);
try {
return input.nextInt();
} catch( InputMismatchException e) {
throw new NotANumberException();
}
}
public static void main(String args[]){
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = getIntegerInput();
}
catch(NotANumberException e){
System.out.println(e.notgetMessage());
}
}
}
PS:正如你所说的这是一项作业,你尝试花精力学习Java类型系统和扩展异常class并添加throws签名。这只是帮助您完成作业的原型。
您必须捕获扫描程序 class 实际抛出的异常,然后对其进行处理,例如创建并抛出您的自定义异常。
catch(InputMismatchException e){
throw new NotANumberException(e);
}
包 com.geek.test;
导入java.util.InputMismatchException;
导入java.util.Scanner;
public class 测试 4 {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int grade;
String remark;
try{
System.out.print("Enter Grade: ");
grade = input.nextInt();
}
catch(InputMismatchException e){
System.out.println("You did not input a number. Please try again!");
}
}
}