问题描述
when we have multiple catch for a single try block? and why ?
我的尝试:
What I have tried:
<pre>
try
{
//some code
}
catch(SqlException sqlex)
{
Console.WriteLine("sqlexception is returned")
}
catch(FormatException fx)
{
Console.WriteLine("FormatException is returned")
}
catch(Exception ex)
{
Console.WriteLine("Mainexception is returned");
}
catch
{
Console.WriteLine("exception without any args is returned");
}
推荐答案
void Main()
{
try
{
throw new Exception();
}
catch(SqlException sqlex)
{
Console.WriteLine("sqlexception is returned");
}
catch(FormatException fx)
{
Console.WriteLine("FormatException is returned");
}
catch(Exception ex)
{
Console.WriteLine("Mainexception is returned");
}
catch
{
Console.WriteLine("exception without any args is returned");
}
}
这是你可以尝试的另一个例子。它将打印无效,因为在尝试打开无效连接时抛出InvalidOperationException。
Here's another example you can try. It will print "invalid" because the InvalidOperationException is thrown upon attempt to open the invalid connection.
void Main()
{
try
{
SqlConnection conn = new SqlConnection("");
conn.Open();
}
catch(SqlException sqlex)
{
Console.WriteLine("sqlexception is returned");
}
catch (InvalidOperationException iox)
{
Console.WriteLine("invalid");
}
catch(FormatException fx)
{
Console.WriteLine("FormatException is returned");
}
catch(Exception ex)
{
Console.WriteLine("Mainexception is returned");
}
catch
{
Console.WriteLine("exception without any args is returned");
}
}
但是,如果你删除了InvalidOperationException catch,那么它将归结为Mainexception is返回。
However, if you removed the InvalidOperationException catch then it would fall down to "Mainexception is returned".
当我们有一个try块的多个catch时,首先执行哪个catch块?
Which catch block is executed first when we have multiple catch for a single try block?
取决于应用程序代码生成的异常类型。由于您正在捕获一些异常
类型,然后:
(a)当您的SQL Server发出警告或错误然后 SqlException
catch块get执行。
(b)当你试图转换形式不好的东西,或者格式为参数无效,然后执行 FormatException
catch块。
(c)当出现空引用或无效操作时,它将转到异常
catch块,因为你没有专门处理 NullReferenceException
和 InvalidOperationException
。 异常
类型通常用于处理一般异常。
Depending on the type of exception your application code generates. Since you were catching a few Exception
types then:
(a) When your SQL Server throws a warning or error then the SqlException
catch block get's executed.
(b) When you are trying to convert something that is not well formed, or the format of an argument is invalid then the FormatException
catch block gets executed.
(c) When a null reference or invalid operation occurred then it will goes to the Exception
catch block since you didn't specifically handle NullReferenceException
and InvalidOperationException
. The Exception
type is typically used to handle generic exceptions.
为什么?
多个 Catch
块通常用于处理不同种类的例外
。就像你的例子一样: SqlException
, FormatException
和 Exception
类型(请记住,有许多类型的例外情况取决于您尝试执行的操作)。
异常是在执行应用程序期间发生的一种错误。 错误
通常是未经发生的问题。然而,异常
预计会在应用程序代码中发生,这就是为什么处理预期异常并专门捕获它们以便轻松识别导致异常发生的原因。处理特定异常在执行某些日志记录时非常有用,并允许应用程序将控制权从代码的一部分转移到另一部分。
Multiple Catch
block are typically used to handle different kinds of Exception
. Just like in your example: SqlException
, FormatException
and Exception
types (keep in mind that there are many types of Exceptions depending on what operation you are trying to do).
Exceptions are a type of error that occurs during the execution of an application. Errors
are typically problems that happens unexpctedly. Whereas, Exceptions
are expected to happen within application code that's why it is important to handle expected exceptions and catch them specifically to easily identify which of which causes the exception to happen. Handling specific exceptions is very useful when doing some logging and allows an application to transfer control from one part of the code to another.
这篇关于首先执行哪个catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!