[英]Try - Catch Exception issue
我正在嘗試制作具有不同選項(十六進制、二進制、八進制、十進制)的桌面計算器,如果輸入的值不屬於該模式,則會出現錯誤,例如“預期為 BinaryInteger,用戶輸入 909382 "並再次提示。 由於我的捕獲(異常 e),現在出現的唯一錯誤是“無效選項”。 如何為無效輸入編寫 4 個異常/錯誤。
import java.util.*;
public class IntDriver
{
//declared fields
private static LongInteger num1;
private static LongInteger num2;
private static String opr;
private static int mode = 0;
// Operators that will be used
private final static String[] Operators = { "", "+", "-", "*", "/" };
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
boolean run = true;
while(run)
{
displayMenu();
while(true)
{
try
{
System.out.print("Option or value --> ");
String option = kb.nextLine();
switch(option.toLowerCase())
{
//modes
// decimal mode
case "decimal":
case "dcm":
case "dec":
mode = 0;
break;
// binary mode
case "binary":
case "bin":
mode = 1;
break;
//octal mode
case "octal":
case "oct":
mode = 2;
break;
//hex mode
case "hexadecimal":
case "hex":
mode = 3;
break;
//quit
case "q":
case "quit":
System.out.println("Thank you. Have a nice day.");
run = false;
break;
//Operators
//add
case "+":
opr = "+";
break;
//subtract
case "-":
opr = "-";
break;
//multiply
case "*":
opr = "*";
break;
//divide
case "/":
opr = "/";
break;
//equals
case "=":
operate();
break;
default:
LongInteger temp;
//with mode it's in:
switch (mode)
{
case 1:
temp = new BinaryInteger(option);
break;
case 2:
temp = new OctalInteger(option);
break;
case 3:
temp = new HexInteger(option);
break;
default:
temp = new DecInteger(option);
break;
}
// if no num1 = LongInteger temp.
if (num1 == null)
{
num1 = temp;
}
else
{
if (opr == null)
{
throw new UnsupportedOperationException();
}
num2 = temp;
}
break;
}
break;
}
catch (UnsupportedOperationException e)
{
System.out.println("Invalid option; operator not specified.\n");
}
// Invalid option entered.
catch (Exception e)
{
System.out.println("Invalid option.\n");
}
}
}
}
//Menu Display
private static void displayMenu()
{
System.out.println();
switch (mode)
{
case 1:
System.out.println("Binary Mode");
break;
case 2:
System.out.println("Octal Mode");
break;
case 3:
System.out.println("Hexadecimal Mode");
break;
default:
System.out.println("Decimal Mode");
break;
}
if (num1 != null)
{
System.out.print(num1 + "\t");
}
if (opr != null)
{
System.out.print(opr + "\t");
}
if (num2 != null)
{
System.out.print(num2 + "\n");
}
System.out.println("\n\n\tModes:\t\t Operators:");
System.out.println("\tBin - Binary\t\t+");
System.out.println("\tOct - Octal\t\t-");
System.out.println("\tDcm - Decimal\t\t*");
System.out.println("\tHex - Hexadecimal\t/");
System.out.println("\tQ - Quit\t\t=\n");
}
private static void operate() throws Exception
{
if (num1 == null || num2 == null || opr == null)
{
throw new Exception("Not enough numbers.");
}
switch (opr)
{
case "+":
num1 = num1.calcValue(opr, num2, mode);
break;
case "-":
num1 = num1.calcValue(opr, num2, mode);
break;
case "*":
num1 = num1.calcValue(opr, num2, mode);
break;
case "/":
num1 = num1.calcValue(opr, num2, mode);
break;
default:
throw new Exception("Invalid operator.");
}
num2 = null;
opr = "";
}
}
您可以為每種情況擴展java.lang.Exception 4 次:
public class YourCustomException extends Exception {
//copy paste and modify whatever constructors are relevant in your case.
}
.. 然后當您的模式輸入無效時繼續拋出這 4 個異常。 但是,這在您的情況下效率很低。 我認為您的案例很簡單,您可以簡單地執行以下操作:
public class DecInteger {
public DecInteger (String option)
{
if (isNotDecimal(option))
throw new Exception("Input option " + option + " is not a decimal");
}
}
..然后在你的catch塊中你需要做這樣的事情:
catch(Exception ex) {
System.out.println(ex.getMessage);
}
從技術上講,拋出Exception
類型的Exception
不是一個好習慣,最好創建對實際異常更具描述性的子類型,但是您的情況很簡單,這並不重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.