Java运行时异常捕获多个catch块

huangapple go评论73阅读模式
英文:

Java RuntimeException catching multiple catch blocks

问题

我有以下的代码

`

    package com.test.Custom;
    
    public class StreamError 
    {
    
        public static void main(String[] args) 
        {
            try
            {
                String str = "Hello";
                if (!str.equals("Hello"))
                {
                    throw new RuntimeException("None of the Directories Exists-Message failed");
                }
               int inf = ster(9);
                System.out.print(inf);
            }
                catch (RuntimeException stex)
                {
                    //getTrace().addWarning("Failing the message");
                    RuntimeException rme = new RuntimeException("None of the Directories Exists-Message failed", stex);
                    throw rme;
                }
            
        }
        public  static int ster(int intg)
        {  try
           {
            if (intg > 6)
            {
                throw new RuntimeException("None of the files Exists-Message failed");
            }
            else
                return intg;
            }
        catch (RuntimeException ste)
          {
           //getTrace().addWarning("Failing the message");
            RuntimeException pme = new RuntimeException("None of the files  Exists-Message failed", ste);
            throw pme;
          }
            
        }
    }
    `

当我执行这段代码对于整数>6例如9我可以看到它会从两个catch块中都抛出运行时异常 - 即使目录部分是正确的即String str = Hello

    Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
        at com.test.Custom.StreamError.main(StreamError.java:21)
    Caused by: java.lang.RuntimeException: None of the files  Exists-Message failed
        at com.test.Custom.StreamError.ster(StreamError.java:39)
        at com.test.Custom.StreamError.main(StreamError.java:11)
    Caused by: java.lang.RuntimeException: None of the files Exists-Message failed
        at com.test.Custom.StreamError.ster(StreamError.java:31)
        ... 1 more

但是当我用错误的目录比如abcd和正确的整数比如2运行它时它只返回来自目录catch块的运行时异常

    Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
        at com.test.Custom.StreamError.main(StreamError.java:22)
    Caused by: java.lang.RuntimeException: None of the Directories Exists-Message failed
        at com.test.Custom.StreamError.main(StreamError.java:13

)
我是否漏掉了一些非常基础的东西请帮忙

谢谢
Sugata
英文:

I have the following code

`

package com.test.Custom;
public class StreamError 
{
public static void main(String[] args) 
{
try
{
String str = "Hello";
if (!str.equals("Hello"))
{
throw new RuntimeException("None of the Directories Exists-Message failed");
}
int inf = ster(9);
System.out.print(inf);
}
catch (RuntimeException stex)
{
//getTrace().addWarning("Failing the message");
RuntimeException rme = new RuntimeException("None of the Directories Exists-Message failed", stex);
throw rme;
}
}
public  static int ster(int intg)
{  try
{
if (intg >6)
{
throw new RuntimeException("None of the files Exists-Message failed");
}
else
return intg;
}
catch (RuntimeException ste)
{
//getTrace().addWarning("Failing the message");
RuntimeException pme = new RuntimeException("None of the files  Exists-Message failed", ste);
throw pme;
}
}
}
`

When I execute this for integer >6, e.g 9 I can see it throws Runtime exceptions from both the catch blocks- even though the Directory part is correct , i.e String str = Hello.

Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
at com.test.Custom.StreamError.main(StreamError.java:21)
Caused by: java.lang.RuntimeException: None of the files  Exists-Message failed
at com.test.Custom.StreamError.ster(StreamError.java:39)
at com.test.Custom.StreamError.main(StreamError.java:11)
Caused by: java.lang.RuntimeException: None of the files Exists-Message failed
at com.test.Custom.StreamError.ster(StreamError.java:31)
... 1 more

But, when I run this for wrong directory(say abcd) and correct integer (say 2) it returns only the runtime exception from directory catch block.

Exception in thread "main" java.lang.RuntimeException: None of the Directories Exists-Message failed
at com.test.Custom.StreamError.main(StreamError.java:22)
Caused by: java.lang.RuntimeException: None of the Directories Exists-Message failed
at com.test.Custom.StreamError.main(StreamError.java:13

)
am I missing something very basic? Please help.

Thanks
Sugata

答案1

得分: 1

这是因为,在第一个尝试块中,即使字符串为 "Hello",您也在调用 ster(9)。因此,在 ster(9) 处发生异常,但此异常也中断了第一个尝试语句。因此,它进入了两个 catch 语句(第一个和第二个)。如果您不希望出现这种情况,您可以在尝试语句之外调用此 int inf = ster(9);

英文:

It is because,you are calling the ster (9) even if string is "Hello" in the first try block. So, on ster(9) you have an exception but this exception breaks the first try statement too. So, it enters to the both catch statements (first and second). If you dont want it, you can call this int inf = ster(9); outside of try statements.

答案2

得分: 0

如果你只是为了将异常继续抛出而捕获它,那么在第一次捕获它时就没有意义。这就是你示例中的问题,不要在catch块中抛出异常,只需处理它(记录一条消息或其他操作)。

英文:

If you catch an exception just to throw it further, it makes no sense of catching it in first instance. That is the problem in your example, don't throw the exception in the catch block, just handle it(log a message or something).

huangapple
  • 本文由 发表于 2020年8月25日 14:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定