C# 如何确定异常是否属于特定类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9270023/
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
How to determine if an exception is of a particular type
提问by AnonyMouse
I have a piece of try catch code:
我有一段 try catch 代码:
try
{
...
}
catch(Exception ex)
{
ModelState.AddModelError(
"duplicateInvoiceNumberOrganisation", "The combination of organisation and invoice number must be unique");
}
For this piece of code I'm trying to insert a record into a database: The dba has set it up so that the database checks for duplicates and returns an error if there are duplicates. Currently, as you can see, I'm adding the same error to the model no matter what error occurred. I want it changed so this error is only added to the model if it was caused by the duplicate error set up by the dba.
对于这段代码,我试图将一条记录插入到数据库中:dba 已对其进行设置,以便数据库检查重复项并在存在重复项时返回错误。目前,如您所见,无论发生什么错误,我都会向模型添加相同的错误。我希望它改变,所以如果这个错误是由 dba 设置的重复错误引起的,那么这个错误只会添加到模型中。
Below is the error I want to catch. Note it's in the inner exception. Can anyone tell me how to specifically catch this one?
下面是我想捕捉的错误。请注意,它位于内部异常中。谁能告诉我如何具体抓住这个?


采纳答案by Davide Piras
beforeyour current catch add the following:
在您当前的捕获之前添加以下内容:
catch(DbUpdateException ex)
{
if(ex.InnerException is UpdateException)
{
// do what you want with ex.InnerException...
}
}
From C# 6, you can do the following:
从 C# 6 开始,您可以执行以下操作:
catch(DbUpdateException ex) when (ex.InnerException is UpdateException)
{
// do what you want with ex.InnerException...
}
回答by Ann B. G.
You can take a look at the SQLException class -- and check for the contents of the exception's message if it contains what you now see in your inner exception..Something like this:
您可以查看 SQLException 类——并检查异常消息的内容,如果它包含您现在在内部异常中看到的内容..像这样:
try
{
//your code here
}
catch (SQLException ex)
{
if (ex.Message.Contains("Cannot insert duplicate key in obj...."))
{
//your code here
}
}
回答by Nishantha
Replace System.Threading.ThreadAbortExceptionwith your exception.
替换System.Threading.ThreadAbortException为您的例外。
try
{
//assume ThreadAbortException occurs here
}
catch (Exception ex)
{
if (ex.GetType().IsAssignableFrom(typeof(System.Threading.ThreadAbortException)))
{
//what you want to do when ThreadAbortException occurs
}
else
{
//do when other exceptions occur
}
}
回答by T.Todua
do yo mean
你是什么意思
catch (Exception e){
if ( e.GetType() == XyzException)
//if (e.GetType().ToString() == "XyzException")
//if (e.GetType().Name == .....)
}
回答by Uday Desiraju
To get name of the exception you can use
要获取您可以使用的异常名称
catch (Exception exc){
if (exc.GetType().FullName == "Your_Exception")
{
// The same can be user for InnerExceptions
// exc.InnerException.GetType().FullName
}
}
回答by Jason
Not enough rep to comment. In response to @conterio question (in @Davide Piras answer):
没有足够的代表发表评论。回应@conterio 问题(在@Davide Piras 回答中):
is there a catch "when not" syntax?
是否有“何时不”语法?
There is.
有。
catch (Exception e) when (!(e is ArgumentException)) { }

