首先执行哪个catch块 [英] Which catch block is executed first

查看:79
本文介绍了首先执行哪个catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
}

推荐答案

它们会通过,因此将执行与Exception类型匹配的第一个(从顶部开始)其他人不会。



此外,你的最后一次捕获永远不会被击中。



您可以通过运行来证明这一点:

They fall through so the first one (from the top) that matches the Exception type will be executed and the others will not be.

Also, your last catch will never be hit.

You can prove this by running:
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".


只是为了添加现有的解决方案...



Just to add up to the existing solution provided...

Quote:

当我们有一个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块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆