主函数的抛出不捕获错误。

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

Throws for main function doesn't catch error

问题

I am having a problem understanding the reason why for Thread.sleep(), the InterruptedException error is caught by the JVM and for ThreadExecp.fun() that is not the case.

我不明白为什么对于Thread.sleep()JVM会捕获InterruptedException错误而对于ThreadExecp.fun()则不是这种情况

public class ThreadExecp {
static void fun() throws InterruptedException
{
System.out.println("Inside fun(). ");
throw new InterruptedException ();
}

public static void main(String args[]) throws InterruptedException {

Thread.sleep(10000);
System.out.println("done 1");
ThreadExecp.fun();
System.out.println("Done");

}
}
Where are actually the errors that occur inside the main method caught and why some are that can simply be thrown and some need to be caught inside the try-catch?

在主方法内部实际上捕获了哪些错误为什么有些错误可以直接抛出而有些需要在try-catch块内捕获
英文:

I am having a problem understanding the reason why for Thread.sleep(), the InterruptedException error is caught by the JVM and for ThreadExecp.fun() that is not the case.

public class ThreadExecp {
static void fun() throws InterruptedException
{
    System.out.println("Inside fun(). ");
    throw new InterruptedException ();
}

public static void main(String args[]) throws InterruptedException {

    Thread.sleep(10000);
    System.out.println("done 1");
    ThreadExecp.fun();
    System.out.println("Done");

}
}

Where are actually the errors that occur inside the main method caught and why some are that can simply be thrown and some need to be caught inside the try-catch?

答案1

得分: 1

"InterruptedException" 在这里没有被捕获,它只是沿着调用堆栈传播。而且在你的代码中没有准备好捕获和处理它的部分,JVM 没有其他选择,只能处理未处理的异常。

英文:

InterruptedException is not caught anywhere here, it's simply propogated along the call stack. And there's nothing in your code ready to catch and handle it, the JVM has no other choice but to take care of the unhandled exception.

答案2

得分: 1

主函数中的抛出不会捕获错误

没有'throws'或'throw'能捕获异常/错误。只有'catch'能捕获异常/错误。

方法声明中的'throws'子句的目的是声明该方法可能抛出指定的异常。

然后,调用这种方法的代码有两种选择:(1) 捕获指定的异常,或者(2) 通过'throws'声明可能传递该异常。

以此类推,一直回溯到调用链的起点。

责任必须落在'main'上。要么'main'有一个异常处理程序(try-catch语句)来处理异常,要么它将异常传递给其调用者,粗略地说就是JVM本身。

在你的情况下,有两个不同的原因可能导致'main'看到InterruptedException - 对fun()的调用和对sleep()的调用。这两者都被声明为可能引发该异常。

你问题的陈述显示了一些混淆。我认为你可能误解了异常是如何沿着调用堆栈向上传播直到找到处理程序的方式。在你的示例main中,根本没有处理程序。

就个人而言,我认为在main上使用'throws'是一种不好的风格。这意味着你的程序知道某个异常是可能的,而且将由JVM来处理。你应该编写一个异常处理程序,明确执行你认为适当的操作。在你的示例中,异常处理程序可能只是打印"interrupted",这似乎符合代码的其余部分。

英文:

> Throws for main function doesn't catch error

No 'throws' or 'throw' catches an exception/error. Only 'catch' catches an exception/error.

The point of a 'throws' clause on a method declaration is to state that this method may throw the stated exceptions.

The caller of such a method then has two choices for its coding: (1) catch the stated exception, or (2) declare through a 'throws' that it may pass on that exception.

And so on, back up the call chain.

The buck has to stop with 'main'. Either 'main' has an exception handler (try-catch statement) for the exception, or else it passes it along ('throws') to its caller, which loosely speaking is the JVM itself.

In your case there are two distinct reasons why your 'main' might see an InterruptedException - the call to fun() and the call to sleep(). Both of these are declared as potentially throwing that exception.

The statement of your question shows a little confusion. I think perhaps you're misunderstanding the way in which exceptions are propagated back up the call stack until a handler is located. And in your example main, there is no handler at all.

Personally, I think it's poor style to put a 'throws' on main. It means that your program knows a certain exception is possible and is going to leave it to the JVM to do something about it. You should rather write an exception handler that explicitly does whatever you consider appropriate. In your example case, the exception handler might just print "interrupted", for example - that seems to fit with the rest of the code.

huangapple
  • 本文由 发表于 2020年8月3日 04:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220477.html
匿名

发表评论

匿名网友

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

确定