Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对各种异常使用try-catch块(java)_Java_Try Catch - Fatal编程技术网

对各种异常使用try-catch块(java)

对各种异常使用try-catch块(java),java,try-catch,Java,Try Catch,因此,我试图在我的Postfix表达式计算器中生成几个try-catch语句,以捕获将从输入文件中出现的异常。我只是需要帮助做那些try-catch语句 因此,电流输出为: 5 2 1 8 -4 2 -4 Exception in thread "main" java.util.EmptyStackException at LinkedStack.pop(LinkedStack.java:44) at PostFix.postfixEvaluation(PostFix.java:

因此,我试图在我的Postfix表达式计算器中生成几个try-catch语句,以捕获将从输入文件中出现的异常。我只是需要帮助做那些try-catch语句

因此,电流输出为:

5
2
1
8
-4
2
-4
Exception in thread "main" java.util.EmptyStackException
    at LinkedStack.pop(LinkedStack.java:44)
    at PostFix.postfixEvaluation(PostFix.java:171)
    at PostFix.readFromTheFile(PostFix.java:49)
    at PostFix.main(PostFix.java:21)
虽然我希望它输出:

5
2
1
8
-4
2
-4
ERROR: java.lang.RuntimeException: Bad character input: " "
ERROR: java.util.EmptyStackException for 1+1
ERROR: java.util.EmptyStackException for 11+*
ERROR: java.util.EmptyStackException for +11
ERROR: java.lang.RuntimeException: Bad character input: "@"
输入文件包含:

   511+93/2-932/-149657%/*-+14+96*5/7%-149-+6*57%/     1+111+*+1111@
这是我当前的代码方法,try-catch块将显示在这里:

  public static Integer postfixEvaluation(String input){



      StackInterface<Integer> stack = new LinkedStack<Integer>();

      Integer result = new Integer(0);



      for(int i=0;i<input.length();i++){

         String characterNum = input.substring(i, i+1);

         Integer integer1 = new Integer(0);
         Integer integer2 = new Integer(0);






         if (characterNum.equals("0")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);

         }
         else if (characterNum.equals("1")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);

         }
         else if (characterNum.equals("2")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("3")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("4")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("5")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("6")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("7")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("8")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("9")) {
            Integer character = (Integer.valueOf(characterNum));
            stack.push(character);
         }
         else if (characterNum.equals("*")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 * integer2;


            stack.push(result);



         }
         else if (characterNum.equals("/")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

            result = integer1 / integer2;


            stack.push(result);

         }
         else if (characterNum.equals("%")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 % integer2;


            stack.push(result);
         }
         else if (characterNum.equals("+")) {
            integer2 = stack.pop();
            integer1 = stack.pop();


            result = integer1 + integer2;


            stack.push(result);

         }
         else if (characterNum.equals("-")) {
            integer2 = stack.pop();
            integer1 = stack.pop();

            result = integer1 - integer2;


            stack.push(result);

         }




      }
     return result;
   }
public静态整数postfix求值(字符串输入){
StackInterface stack=新建LinkedStack();
整数结果=新整数(0);

对于(int i=0;i当您首先获得任何字符时,请检查字符是否为空,如果是,则通过自定义异常

if(characterNum.equals("")){
  throw new BadCharacterException(" Bad character input: \" \"");
}

如果我理解你的话,你不想把你的异常分组在应用程序的末尾。我认为你可以像平常一样捕获你的异常(例如atish shimpi answare),然后在捕获部分通过将它们推到某种fifo集合来处理异常。在处理完整个输入后,只需打印出来


我会在这里选择checked exception,并在您打印异常的地方处理异常的打印。然后您可以决定是否要停止,如何格式化消息等。

Hm.。其他3个EmptyStackException呢?我现在理解BadCharacterException了!