尝试/捕捉-返回原始尝试 [英] Try/Catch - back to original try

查看:66
本文介绍了尝试/捕捉-返回原始尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一下这个简单的try/catch示例...

Let's look at this simple try/catch example...

try
{
    // User inputs path of a file
    if(!ValidPath)
        throw new InvalidPathException();
}
catch InvalidPathException e
{
    // Log error
    // Re-throw the error.
    throw;     
}

耦合问题.两者可能都是简单的答案.

Couple questions. Both are probably simple answers.

  1. 渔获物中的掷物是否返回并检查下一个渔获物,然后检查下一个渔获物,依此类推?
  2. 有没有办法回到最初的尝试?还是那不好的编程习惯?

推荐答案

  1. 是的,catch块中的throw语句将被任何包装器/下一个catch块重新捕获.

  1. Yes the throw statement in the catch block will be re-catches by any wrapper/next catch block.

使用Basic Goto:like语句修改程序流是不好的编程习惯,因此建议不要返回到原始代码块(这也是不切实际的).另外,自调试器输出&之后,您总是尝试处理异常,使其接近发生的地方.堆栈跟踪将以这种方式提供更多信息.

It is a bad programming practice to modify the program flow using Basic Goto: like statements so it is not advisable to return back to the originating code block (which would also be impractical). Also you always try to handle exceptions close to where they occur since debugger output & stack trace will be much more informative that way.

这篇关于尝试/捕捉-返回原始尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆