抛出新的异常(我的异常); 但是当使用像微软框架库中的其他类时 那行高亮的是打电话给班级 包含该例外的行。 我想说的是,当我的异常被抛出时我不那个 哈哈抛出的豆子,但我不知道是什么让它扔掉了,所以它是b / b 恐惧使用在那里。 任何sugestions。 谢谢,Jay Dee。>I have a query about throwing exceptions.To throw an exception I type something like:try{ // do somthing}catch (ArgumentOutOfRangeException){ throw new Exception("My Exception");}And when the try section fales my excepton is thrown and if not courtelswair will display when debuging and hi-light the line:throw new Exception("My Exception");but when using other classes like in the microsoft framework librarythe line that is hi-lighted is the line that called the classcontaining that exception.What I am trying to say is that when my exception is thrown I no thatit has bean thrown but I don''t know what corsed it to throw, so it isfearly useles being there.Any sugestions.Thanks, Jay Dee. Jay Dee写道:Jay Dee wrote: [...] 当试用部分失效时,我的投掷将被抛出,如果不是法院的话 elswair将在debuging和hi时显示点亮这一行: 抛出新的异常(我的异常); 但是当使用像microsoft框架库中的其他类时 高亮的线是调用包含该异常的类 的线。[...]And when the try section fales my excepton is thrown and if not courtelswair will display when debuging and hi-light the line:throw new Exception("My Exception");but when using other classes like in the microsoft framework librarythe line that is hi-lighted is the line that called the classcontaining that exception. 差别主要是因为你没有.NET代码的源代码 。因此,调试器会向您显示最接近抛出异常的源代码 。如果您自己的代码抛出异常,那么该代码就是抛出异常的代码。但是如果其他一些 代码抛出一个异常,那么最接近的代码仍将在你自己的代码中,在那里它调用了源代码不存在的代码。 br />The difference there is basically because you don''t have the source codeto the .NET code. So the debugger shows you the source code that isclosest to the thrown exception. If your own code throws an exception,then that code is the code that threw the exception. But if some othercode throws an exception, then the closest code will still be in yourown code where it called the code for which the source code is absent. 我想说的是,当我的异常被抛出时我没有那个 它有豆扔了但我不知道什么强迫它抛出,所以它是b / b 恐惧使用在那里。What I am trying to say is that when my exception is thrown I no thatit has bean thrown but I don''t know what corsed it to throw, so it isfearly useles being there. 这是对的。正如阿尔文所说的那样,为什么你这样编写 代码并不清楚。你有一个非常好的例外。如果你不是要处理它(也就是从它中恢复)那么为什么要用 全新的异常隐藏它?特别是一个通用的,而不是原始的一个。 至少,你可以写throw;而不是抛出新的 异常(我的例外);至少这会导致异常 被抛出与被捕获的异常相同。如果我记得, 堆栈跟踪将是错误的,但如果你修复了catch,那么语句,以便 它声明一个本地ArgumentOutOfRangeException变量,然后至少 你可以在调试器中查看该变量并查看堆栈 跟踪是(但是,只有当你在自己的抛出 语句之前放置一个断点时)。 我的经验是它重新抛出 异常或从catch中抛出新异常几乎总是错误的。条款。有时 这是你想做的事情,但通常最好只是在catch中本地处理 错误。条款或放置任何清理代码 认为应该在catch中条款进入终于条款 而不是任何例外。 PeteThat''s right. As Alvin says, it''s not at all clear why you write thecode like that. You had a perfectly good exception. If you''re notgoing to handle it (that is, recover from it) then why hide it with awhole new exception? Especially one that is a generic one, rather thanthe original one.At the very least, you could just write "throw;" instead of "throw newException("My Exception");" At least that would cause the exceptionthrown to be the same as the one that was caught. If I recall, thestack trace will be wrong, but if you fix the "catch" statement so thatit declares a local ArgumentOutOfRangeException variable, then at leastyou can look at that variable in the debugger and see what the stacktrace is (but only if you put a breakpoint before your own "throw"statement, of course).My experience has been that it''s almost always a mistake to rethrow anexception or throw a new exception from a "catch" clause. Sometimesit''s what you want to do, but usually it''s better to either just handlethe error locally in the "catch" clause or put whatever cleanup code youthought should go in the "catch" clause into the "finally" clauseinstead, and just not catch any exception.Pete 这篇关于抛出例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 14:56