在C#,尝试捕获最终如何阻止工作?
所以如果有异常,我知道它将跳转到catch块然后跳转到finally块.
但是,如果没有错误,catch块将不会运行,但finally块是否会运行呢?
我只是看着使用声明,我一直都知道它的作用,但直到现在还没有尝试使用它,我已经提出了以下代码:
using (SqlCommand cmd =
new SqlCommand(reportDataSource,
new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString)))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
cmd.Connection.Open();
DataSet dset = new DataSet();
new SqlDataAdapter(cmd).Fill(dset);
this.gridDataSource.DataSource = dset.Tables[0];
}
Run Code Online (Sandbox Code Playgroud)
这似乎是工作,但有没有因为就在这个任何一点我可以告诉我还需要附上这在try catch块捕捉不可预见的错误,例如SQL服务器停机.我错过了什么吗?
据我目前所知,它只是阻止我关闭和处理cmd,但由于仍需要try catch,因此会有更多行代码.
这是一个简单的Objective-C问题.
当您使用@try工作流程时,可以以2种方式运行
@catch阻止而不是@finally@try块然后运行@finally那么,使用或不使用@finally块有什么区别?如果我只使用:
-(void)function{
@try {
...
}
@catch (NSException *exception) {
...
}
>>>The workflow will run this line in any case?
}
Run Code Online (Sandbox Code Playgroud)
将运行该函数的其余部分,或者仅在@catch创建NSException时阻止该块?
我了解“ finally”关键字在各种语言中的含义,但是,我很难理解为什么您会在口味偏爱格式之外使用它。
例如,在PHP中:
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
finally {
// run this code every single time regardless of error or not
}
Run Code Online (Sandbox Code Playgroud)
这段代码在做什么和这样做之间有什么区别?
try {
possibleErrorThrownFunction();
}
catch (CustomException $customException) {
// handle custom error
}
catch (Exception $exception) {
// handle the error
}
// run this code every single time regardless of error or not
Run Code Online (Sandbox Code Playgroud)
由于捕获了错误,最后一行不是总会运行吗?在这种情况下,finally除非您只想维护代码样式的格式,否则没有实际使用的情况? …
如果我使用
try{
function1();
function2();
function3();
}
catch(e:Error){
function4();
}
Run Code Online (Sandbox Code Playgroud)
假设在function2()中抛出一个异常,哪个代码按定义执行?function3()会被执行吗?捕获后,function1的效果是否会出现?(有些编程语言可以"回放"效果,例如整个块是否未执行)
谢谢你的澄清!
我在我的项目中使用try catch和finally块.我怀疑的是......为什么我们需要使用finally块,实际上如果不使用finally块那么代码也会在catch块之后执行.所以我们可以在catch块之后做代码(做无资源).即使发生异常,这也会执行.那么如果我们使用finally块有什么好处吗?
如果我捕获所有异常;
try
{
... //code causes error
}
catch (Exception e)
{
...//handle all exceptions
}
Run Code Online (Sandbox Code Playgroud)
所以有没有必要使用Finally块?由于我捕获了所有异常,程序会在try-catch后继续执行代码吗?
另一个问题是,如果使用 finally 块,我如何才能捕获在 final 块本身中发生的错误?我的意思是看起来我们只需要把所有东西都放在最后的 try 和 catch 块中?
我们在这个论坛上看到过很多关于try-catch-finally和try-finally建设的问题.
答案的数量增加了问题的数量,所以我也很少.
这是Microsoft解释try-finally构建的链接.我已经读过了!
在下面的文章中写道:
在处理的异常中,保证运行关联的finally块.但是,如果未处理异常,则finally块的执行取决于如何触发异常展开操作.反过来,这取决于您的计算机的设置方式.
我是否正确地理解在try-catch-finally施工finally中将永远执行?(不包括Environment.FastFail())
我已经在这个论坛上读到过StackOverFlowException(这个finally块没有在这个场合执行过),但是当我throw这个时,finally块被执行了.那有什么关系StackOverFlowException?
为什么finally不调用块?(在下面的代码中)?
我们一般使用哪些情况try-finally?
从什么PC设置finally块依赖?
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception();
}
finally
{
Console.WriteLine("finally");
Console.ReadKey();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么最后在C#中使用?
在C#中,有一个finally条款有什么意义?
例如.
try {
// do something
}
catch (Exception exc)
{
// do something
}
// do something
Run Code Online (Sandbox Code Playgroud)
最后代码不会执行吗?一个finally街区有什么意义?
我正在自学 C#,我要学习 try、catch 和 finally。我正在使用的这本书讨论了 finally 块如何运行,而不管 try 块是否成功。但是,即使不在 finally 中,写在 catch 块之外的代码也不会运行吗?如果是这样,最后的意义是什么?这是本书提供的示例程序:
class myAppClass
{
public static void Main()
{
int[] myArray = new int[5];
try
{
for (int ctr = 0; ctr <10; ctr++)
{
myArray[ctr] = ctr;
}
}
catch
{
Console.WriteLine("Exception caught");
}
finally
{
Console.WriteLine("Done with exception handling");
}
Console.WriteLine("End of Program");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)