在c#中确定try.catch ...中的异常类型 [英] Determine exceptions type in try... catch... in c#
本文介绍了在c#中确定try.catch ...中的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想确定我的catch
块内的Exception
类型:
I want to determine the Exception
type inside my catch
block:
try
{
// ...
}
catch (Exception ex)
{
if (ex == UnauthorizedAccessException) // ??
{
// ...
}
else
{
// ...
}
}
推荐答案
您可以尝试下面的代码.
You can try like bellow code.
try
{
// do something that could throw an exception
}
catch(Exception e)
{
if (e is FileNotFoundException)
{
// •••
}
else if (e is DivideByZeroException)
{
// •••
}
else throw;
}
谢谢
Thanks
您可以使用GetType()函数来确定在Catch块中生成哪个异常.
You can use GetType() function to determine which exception is generate in Catch block.
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.GetType().ToString()); // It will Display Message type
}
:)
:)
try
{
// ...
}
catch (UnauthorizedAccessException)
{
// ...
}
catch (Exception)
{
// ...
}
像这样的东西.
Something like this.
这篇关于在c#中确定try.catch ...中的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文