1

我正在创建一个Pair用于整数的泛型类。

异常类:

public class WrongThing extends Exception{
    public WrongThing(String message) {
        super(message);
    }
}

主类:

public class Pair<E> implements Comparable<E>{
    private E var1;
    private E var2;
    public Pair(E var1, E var2) throws WrongThing{
        //this is on purpose!!!
        System.out.println("var1 and var2 are switched");
        this.var1 = var2;
        this.var2 = var1;
    }

    void get(){
        System.out.println("the first actualy 2nd: "+
                var1 + "the ");
                System.out.println("  second actualy first" + var2);
    }

    void set1(E temp){
        System.out.println("var1 and var2 are switched");
        temp = var1;
    }

    void set2(E temp){
        System.out.println("var1 and var2 are switched");
        temp = var2;
    }

    E smallest(E var1, E var2){

        return var1;
    }

    @Override
    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
}

测试用例

import java.util.Scanner;
import java.util.InputMismatchException;

public class PairTest {
    public static void main(String[] args) throws WrongThing{
        System.out.println("two integers please");
        Scanner sc = new Scanner(System.in);
        Pair<Integer> newPair;
        Pair<Integer> tempPair1= new Pair<Integer>(3,2);

        try{
            newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
            //throw new InputMismatchException("that is not an Integer....");
        }catch(WrongThing exception){
            //System.out.println("you cant do that. try again and press enter after the first integer");
            newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
            newPair.get();

        }
        finally{


        }
    }
    //newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
}

当我运行此代码时,我得到一个InputMismatchException. 我是否没有正确创建异常或在抛出异常时没有捕获它?

4

6 回答 6

1

除非我遗漏了某些东西,否则您似乎只是在尝试捕获WrongThingException,而不是InputMismatchEception.

try{
    // ...
}catch(WrongThing exception){
    // ...
}
于 2012-10-25T06:44:50.837 回答
1

WrongThing 是您的自定义异常。必要时,您需要将其扔掉并接住。

try{
    // throw new WrongThing("wrong thing");
}catch(WrongThing e1){
    // ...
}catch(InputMismatchException e2){
    // ...
}

InputMismatchExceptionsc.nextInt()方法抛出if the next token does not match the Integer regular expression, or is out of range。所以你也需要缓存它。

于 2012-10-25T06:49:36.723 回答
0

当您创建对类的对象时

Pair newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());

它只能抛出自定义异常,因为您在课堂上提到了这种类型的异常。sc.nextInt()期望来自控制台的 Integer 类型的 Input ,当传递任何其他字符时,它会抛出InputMismatchException. 所以你还需要像这样捕获 InputMismatch Exception

try{
    // throw new WrongThing("wrong thing");
}catch(WrongThing e1){
    // ...
}catch(InputMismatchException e2){
    // ...
}
于 2012-10-25T07:19:07.167 回答
0

扫描仪正在抛出您未处理的 InputMismatchException。InputMismatchException是一个 RuntimeException,因此您无需显式捕获它。(通常是程序员错误)如果您使用的是 Java 7,那么您可以使用这种语法(multicatch)来处理多个异常

try{
....
}
catch(WrongThing|InputMismatchException e1){
        //deal with exception.
}

这假设您想以相同的方式处理这两个异常。如果您不这样做,那么我建议您将它们分开,因为其他答案已为清晰起见。

于 2012-10-25T07:48:38.973 回答
0

对于 Scanner 类,您必须遵循流程 hasNext() -> next()

你不会以这种方式得到任何异常。

   Scanner sc = new Scanner(System.in);       
          while (sc.hasNext()) {

                 if(sc.hasNextInt())
                System.out.println(sc.nextInt());

              sc.next();
          }
         sc.close();

另一件事是catch(Exception e)在底部有一个级联的异常块,以确保捕获所有异常。

   try{
         // code which throws checked exceptions.
        }catch(WrongThing e1){      
            e1.printStackTrace();
        }catch(Exception e2){
            e2.printStackTrace();
        }finally{
           // cleanup
       }
于 2012-10-25T07:03:03.100 回答
0

的文档java.util.InputMismatchException说:

由 a 抛出Scanner以指示检索到的令牌与预期类型的​​模式不匹配,或者该令牌超出预期类型的​​范围。

一个Scanner.nextInt()说:

抛出: - 如果下一个标记与正则表达式InputMismatchException不匹配,或者超出范围Integer

Scanner.nextInt()基本上,当您在控制台中键入不是整数的内容时会引发异常。发生这种情况时,new Pair<Integer>(...)甚至不会调用构造函数,因此您在构造函数中进行的任何检查都将变得无用。

你需要做的是

newPair = null;
while (newPair == null) {
    try {
        newPair = new Pair<Integer>(sc.nextInt(),sc.nextInt());
    } catch(InputMismatchException exception){
        System.out.println("you cant do that. try again and press enter after the first");              
    }
}
于 2012-10-25T07:11:33.490 回答