C# Try/Catch 块未捕获异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1630437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Try/Catch block not catching Exception
提问by avesse
I have a statement inside a try/catch block, but the exception is not getting caught. Can anyone explain?
我在 try/catch 块中有一条语句,但未捕获异常。谁能解释一下?
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
Source Error:
源错误:
Line 139: try
Line 140: {
Line 141: return (int)Session["SelectedLeadID"];
Line 142: }
Line 143: catch (Exception ex)
UpdateThis is an ASP.NET application. In the catch block, a new exception is thrown. The code you see is what is displayed on the ASP.NET error page.
更新这是一个 ASP.NET 应用程序。在 catch 块中,抛出了一个新的异常。您看到的代码就是显示在 ASP.NET 错误页面上的代码。
回答by cwap
That catch block should catch the exception, but make sure there's no re-throwing in there.
该 catch 块应该捕获异常,但要确保那里没有重新抛出。
Another small comment: I've been tricked quite a few times by VS, cause it breaks on exceptions like that while running in debug-mode. Try to simply press 'continue' or 'F5' and see if your application doesn't work anyway :)
另一个小评论:我已经被 VS 欺骗了很多次,因为它在调试模式下运行时会因为这样的异常而中断。尝试简单地按“继续”或“F5”,看看您的应用程序是否仍然不起作用:)
回答by Marc Gravell
I suspect you're going to need to add more detail - that isn't reproducible just from your code. In particular (as already noted) we'd need to see inside the catch
, and verify that the exception is actually being thrown from insidethe try
and not somewhere else.
我怀疑您将需要添加更多细节 - 这不能仅从您的代码中重现。特别是(前面已经提到)我们需要看到里面的catch
,并验证异常实际从抛出内部的try
,而不是别的地方。
Other possibilities:
其他可能性:
- you have dodgy code inside the exception handler that is itself throwing an exception
- you have a dodgy
Dispose()
that is getting called (using
etc) - you are in .NET 1.1 and the thing getting thrown (in code not shown) isn't an
Exception
, but some other object
- 你在异常处理程序中有狡猾的代码,它本身会抛出异常
- 你有一个狡猾的人
Dispose()
被称为(using
等) - 您在 .NET 1.1 中,并且抛出的东西(在未显示的代码中)不是
Exception
,而是其他一些对象
回答by Frank Bollack
If it is only the debugger breaking on the exception and you are using VS2005 or above, you might want to check under Debug->Exceptions...if any of the Common-Language-Runtime-Exceptions are activated. If so, the debugger will always catch the exceptions first, but you are allowed to continue.
如果只是调试器中断了异常并且您使用的是 VS2005 或更高版本,您可能需要在Debug->Exceptions...下检查是否有任何 Common-Language-Runtime-Exceptions 被激活。如果是这样,调试器将始终首先捕获异常,但您可以继续。
To revert to normal execution, simply uncheck the apropriate exceptions from the list.
要恢复正常执行,只需从列表中取消选中相应的异常即可。
回答by mike
The code looks terribly ugly IMO. For there to be something in the catch() block means you are going to have another return ...
statement which AFAIK you should always have a single return statement at the end of each function to make following code easier.
代码看起来非常难看 IMO。因为在 catch() 块中有一些东西意味着你将有另一个return ...
语句,AFAIK 你应该总是在每个函数的末尾有一个 return 语句,以使下面的代码更容易。
i.e. maybe your code should look like
即也许你的代码应该看起来像
public int Function()
{
int leadID = 0;
try
{
int leadID = (int)Session["SelectedLeadID"];
}
catch (Exception ex)
{
...
}
return leadID
}
Single exit points are supposed to make the code easier to follow, I guess? Anyway, to get any useful help you have to post more of the function.
单个退出点应该使代码更容易理解,我猜?无论如何,要获得任何有用的帮助,您必须发布更多功能。
回答by Marco Rodrigues
C# - Visual Studio 2019
C# - Visual Studio 2019
Real case solution:
真实案例解决方案:
Use "System.Exception" instead of Exception.
--> catch (System.Exception ex)
使用“System.Exception”代替异常。-->catch (System.Exception ex)
I had this exact same problem ( not entering the exception ). The probem was caused by a "using Java.Lang;" We are using ANDROID SDK inside C#, in this particular project.
我遇到了完全相同的问题(未输入异常)。该问题是由“使用 Java.Lang;”引起的。在这个特定项目中,我们在 C# 中使用 ANDROID SDK。
C# - Visual Studio 2019
C# - Visual Studio 2019