简体   繁体   English

中止进一步的程序执行

[英]Abort further program execution

I have a several nested methods in my program. 我的程序中有几个嵌套方法。 If a deeply nested method errors out in my try block how do i stop further execution and print the error to the console? 如果深度嵌套的方法在我的try块中出错,我如何停止进一步执行并将错误打印到控制台?

Not sure whether I'm understanding your question correctly... 不确定我是否正确理解你的问题......

But if you don't want to handle it in the deeper-down code, then don't catch it (or catch it and re-throw it again). 但是如果你不想在更深层次的代码中处理它,那么就不要抓住它(或者抓住它并再次重新抛出它)。

Exceptions travel up the call stack until something catches them. 异常会在调用堆栈中向上移动,直到有东西捕获它们为止。

You can rethrow the exception within your catch block using throw; 您可以使用throw;catch块中重新抛出异常throw; . If you let this bubble to the top, your app can handle it and exit. 如果您将此气泡置于顶部,您的应用可以处理它并退出。

你可以做的另一件事是将Exception包装在一个自定义异常中,并指示你的不同try catchs重新抛出它,直到它到达你想用来处理它的一个特定的try catch块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM