尝试/抓住Java [英] Try/catch in Java

查看:158
本文介绍了尝试/抓住Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个提示,为什么这个尝试和抓住不起作用?
它抛出一个扫描器异常,而不是打印我预期的消息。

  import java.util。*; 
import java.io. *;
import java.math。*;
import javax.swing。*;

public class Main {
public static void main(String [] args){
Boolean test = true;
while(test == true){
try {
double x,y;
字符串运算符;
扫描仪扫描=新扫描仪(System.in);
扫描仪scan_2 =新的扫描仪(System.in);
扫描仪ScanOperator =新扫描仪(System.in);
System.out.println(输入double值:);
x = scan.nextDouble();
System.out.println(输入另一个double值:);
y = scan_2.nextDouble();
System.out.println(输入要执行的操作的操作符,如果要退出,则输入X);
operator = ScanOperator.nextLine();
if(operator.equals(x)|| operator.equals(X)){
test = false;
System.out.println(没有计算完成!!!);
}
System.out.println(Calculation(operator,x,y));
} catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null,Input must be a number。);
}
}
}

public static double Calculation(String operator,double x,double y){
double result = 0;
double myAdd = 0;
double mySub = 0;
double myMult = 0;
double myDiv = 0;
double myPower = 0;
double myMod = 0;

if(operator.equals(+)){
myAdd = x + y;
result = myAdd;
} else if(operator.equals( - )){
mySub = x - y;
result = mySub;
} else if(operator.equals(*)){
myMult = x * y;
result = myMult;
} else if(operator.equals(/)){
myDiv = x / y;
result = myDiv;
} else if(operator.equals(^)){
myPower = Math.pow(x,y);
result = myPower;
} else if(operator.equals(%)){
myMod = x%y;
result = myMod;
} else {
}

返回结果;
}
}


解决方案

简单,程序会抛出ScannerException,但是你的try catch只能捕获NumberFormatException,你需要添加另一个catch子句才能捕获ScannerException,或者只捕捉一般的异常。



例如,当您说:

 } catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null,Input必须是一个数字。);
}

只是指定如何捕获NumberFormatException。

为了捕获所有异常,您需要进行以下操作:

 } catch(NumberFormatException nfe){
JOptionPane .showMessageDialog(null,Input must be a number。);
} catch(Exception e){
JOptionPane.showMessageDialog(null,Generic exception catch);
}

在这种情况下,第二个catch将获得在首先捕获因为所有异常都扩展了Exception类,你可以用该语句捕获所有派生类。



但是,由于捕获异常本身是皱眉的,你也可以做:

 } catch(NumberFormatException,ScannerException e){
JOptionPane.showMessageDialog(null,Input must be a number 。);
}

在同一个块中捕获两个异常。


Could someone please give me a hint why this try and catch is not working? It throws a scanner exception instead of printing the message I expect.

import java.util.*;
import java.io.*;
import java.math.*;
import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        Boolean test = true;
        while (test == true) {
            try {
                double x, y;
                String operator;
                Scanner scan = new Scanner(System.in);
                Scanner scan_2 = new Scanner(System.in);
                Scanner ScanOperator = new Scanner(System.in);
                System.out.println(" Enter a double value: ");
                x = scan.nextDouble();
                System.out.println(" Enter another double value: ");
                y = scan_2.nextDouble();
                System.out.println(" Enter a operator for the operation you want to execute, or X if you want to quit: ");
                operator = ScanOperator.nextLine();
                if (operator.equals("x") || operator.equals("X")) {
                    test = false;
                    System.out.println("No calculation was made!!!");
                }
                System.out.println(Calculation(operator, x, y));
            } catch (NumberFormatException nfe) {
               JOptionPane.showMessageDialog(null,"Input must be a number.");
            }
        }
    }

    public static double Calculation(String operator, double x, double y) {
        double result = 0;
        double myAdd = 0;
        double mySub = 0;
        double myMult = 0;
        double myDiv = 0;
        double myPower = 0;
        double myMod = 0;

        if (operator.equals("+")) {
            myAdd = x + y;
            result = myAdd;
        } else if (operator.equals("-")) {
            mySub = x - y;
            result = mySub;
        } else if (operator.equals("*")) {
            myMult = x * y;
            result = myMult;
        } else if (operator.equals("/")) {
            myDiv = x / y;
            result = myDiv;
        } else if (operator.equals("^")) {
            myPower = Math.pow(x, y);
            result = myPower;
        } else if (operator.equals("%")) {
            myMod = x % y;
            result = myMod;
        } else {
        }

        return result;
    }
}

解决方案

Simple, the program throws ScannerException, but your try catch can only catch NumberFormatException, you need to add another catch clause in order to catch ScannerException, or catch only the generic Exception.

for example, when you say:

 } catch (NumberFormatException nfe) {     
     JOptionPane.showMessageDialog(null,"Input must be a number.");
 }

that is only specifying how to catch NumberFormatException.
In order to catch all exceptions, you would need to make it:

 } catch (NumberFormatException nfe) {     
     JOptionPane.showMessageDialog(null,"Input must be a number.");
 }catch (Exception e){
     JOptionPane.showMessageDialog(null,"Generic exception caught");
 }

In this case, the second catch would get everything that was not caught in the first catch because all exceptions extend the Exception class, you can catch all derived classes with that statement.

However, since catching Exception by itself is frowned upon, you could also do:

 } catch (NumberFormatException, ScannerException e) {     
     JOptionPane.showMessageDialog(null,"Input must be a number.");
 }

To catch both exceptions in the same block.

这篇关于尝试/抓住Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆