gpt4 book ai didi

java - 如何在 Java 中抛出正确的异常

转载 作者:行者123 更新时间:2023-12-01 13:28:22 30 4
gpt4 key购买 nike

当输入无效运算符(例如 $、# &)时,我尝试抛出异常。当数字除以零时也是如此。尽管该应用程序正在崩溃。我想知道我这样做是否正确。

封装PrefixCalc;

导入java.util.Scanner;

public class Expressions {

public static class IllegalExpressionException extends RuntimeException {

public IllegalExpressionException(String message) {
super("Illegal expression: " + message);
}
}

private static class Calculator {

private final boolean Calc; // ?Is this a Calc? else internal
private final char op; // For an internal node, the operator
private double value; // For a Calc, the value
private Calculator left, // Left subexpression (internal node)
right; // Right subexpression
// Bare-bones constructor

private Calculator(boolean Calc, char op, double value) {
this.Calc = Calc;
this.op = op;
this.value = value;
this.left = null; // Empty to start
this.right = null;
}
// For Calc nodes, show the value; for internal, the operator.

public @Override
String toString()// Overrides Object.toString, must be public.
{
return Calc ? Double.toString(value) : Character.toString(op);
}
}
Calculator root = null;

public Expressions(Scanner input) {
root = build(input);
}

/**
* Based on a white-space delimited prefix expression, build the
* corresponding binary expression tree.
*
* @param input The scanner with the expression
* @return reference to the corresponding binary expression
*/
private Calculator build(Scanner input) {

boolean Calc;
String token;
double value;
Calculator node;
Calc = input.hasNextDouble();
if (Calc) {
try {
value = input.nextDouble();
node = new Calculator(Calc, '\0', value);
} catch (IllegalExpressionException message) {
throw new IllegalExpressionException("Illegal Expression Syntex!");
}
} else {
try {
token = input.next();
node = new Calculator(Calc, token.charAt(0), 0.0);
node.left = build(input);
node.right = build(input);
} catch (IllegalExpressionException message) {
throw new IllegalExpressionException("Illegal Expression Syntex!");

}

}
return node;


}

/**
* Show the expression tree as a prefix expression.
*/
public void showPreFix() {
showPreFix(root);
System.out.println();
}
// Prefix expression is the result of a pre-order traversal

private void showPreFix(Calculator node) {
while (node != null) {
System.out.print(node + " ");
showPreFix(node.left);
node = node.right; // Update parameter for right traversal
}
}

/**
* Show the expression tree as a parenthesized infix expression.
*
*/
public void showInFix() {
showInFix(root);
System.out.println();
}
// Parenthesized infix requires parentheses in both the
// pre-order and post-order positions, plus the node
// itself in the in-order position.

private void showInFix(Calculator node) {
if (node != null) {

if (!node.Calc) {
System.out.print("( "); // Pre-order position
}
showInFix(node.left);
System.out.print(node + " "); // In-order position
showInFix(node.right);
if (!node.Calc) // Post-order position
{
System.out.print(") ");
}
}
}


public double evaluate() {
return root == null ? 0.0 : evaluate(root);
}


private double evaluate(Calculator node) throws ArithmeticException {
double result; // Value to be returned
if (node.Calc) // Just get the value of the Calc
{
result = node.value;
} else {

double left, right;
char operator = node.op;
if(operator != '-' && operator != '+' && operator != '/' && operator != '*') {
throw new ArithmeticException("Illegal Arthematic Syntex!" + operator);
// break;
}
// Capture the values of the left and right subexpressions
left = evaluate(node.left);
right = evaluate(node.right);
// Do the arithmetic, based on the operator
switch (operator) {
case '-':
result = left - right;
break;
case '*':
result = left * right;
break;
case '+':
result = left + right;
break;
case '/':

result = left / right;

if (right == 0) {
throw new ArithmeticException("Division by zero!");
}
break;

default:
throw new ArithmeticException("Illegal Arthematic Operation" + operator);
}
}
// Return either the Calc's value or the one we just calculated.
result = Math.round(result * 100.0) / 100.0;
return result;

}
}

输入无效运算符时的输出为:

线程“main”中出现异常java.lang.ArithmeticException:非法算术语法!$ 在 PrefixCalc.Expressions.evaluate(Expressions.java:152) 在 PrefixCalc.Expressions.evaluate(Expressions.java:135) 在 PrefixCalc.PrefixCalc.main(PrefixCalc.java:25)Java 结果:1

最佳答案

从您的代码中我看到 Java 运行时已引发异常。您必须处理它们并向用户显示适当的错误消息。

但是,正确的做法是在执行表达式之前验证用户输入。这将避免进行调用、获取异常并稍后进行处理。验证用户输入,如果输入不正确,则向用户显示一条消息。

关于java - 如何在 Java 中抛出正确的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692390/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com