出现异常后,在 finally{} 外的语句也被执行?using System;
public class Test
{
public static void Main()
{
int a= 10;
int b;
int c = 0;
Console.Write("输入一个数:");
b = Convert.ToInt32(Console.ReadLine());
try
{
c = a / b;
}
catch(Exception)
{
Console.WriteLine("除数不能为0!");
}
finally
{
if(c != 0)
{
Console.WriteLine("商为{0}",c);
}
}
Console.WriteLine("最后一句"); //为什莫出现异常也会执行,那finally 还有什末用?
}
}
public class Test
{
public static void Main()
{
int a= 10;
int b;
int c = 0;
Console.Write("输入一个数:");
b = Convert.ToInt32(Console.ReadLine());
try
{
c = a / b;
}
catch(Exception)
{
Console.WriteLine("除数不能为0!");
}
finally
{
if(c != 0)
{
Console.WriteLine("商为{0}",c);
}
}
Console.WriteLine("最后一句"); //为什莫出现异常也会执行,那finally 还有什末用?
}
}
解决方案 »
- Chart Area Axes - The minimum and maximum axis values have not been specified.
- com组件奇怪的问题
- 后台生成html代码,但是生成的代码无法进行post提交的自己的后台的??
- 多线程异常
- 我用的是 vs2008 当我给DropDownList 绑定数据时就会无法显示网页
- 请问,在C#中是否有鼠标拖拽事件,该如何使用?请举例说明,谢谢!
- 用C#实现简单数据传递
- 报表程序中显示“无效的报表源”错误(散分)
- 为什么??
- 急切请教几个c#问题
- 请教,为什么看不到Excel应用程序
- 求救!为什么我用本地SMTP服务器(localhost)发送邮件老是被对方主机拒绝!大侠帮帮忙啊
catch(Exception)
{
throw;
}
调试时能看到错误信息,那最后一句就不执行了。
所以你必须在finally里写一些资源清理的代码。
现在你在catch(Exception)子句里写的是一些普通的错误提示语句,所以finally有没有都无所谓