C# 抛异常的问题

tony-src 2010-12-08 04:32:49
using System;

namespace ConsoleApplication2
{
class DBHelper
{
public void showerr()
{
try
{
Convert.ToInt32("33");
}
catch (Exception)
{
Console.WriteLine("异常");
return;
}
}
}

class Program
{
static void Main(string[] args)
{
DBHelper dbHelper=new DBHelper();
dbHelper.showerr();
Console.WriteLine("执行成功!");

}
}
}

以上代码如何让其类实例抛出异常时,Console.WriteLine("执行成功!");不执行?
除了把try{}catch{}放外面...
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
d563382384 2010-12-08
  • 打赏
  • 举报
回复
Console.Readline()加上 你才能看出效果
itzrx 2010-12-08
  • 打赏
  • 举报
回复

class DBHelper
{
public bool showerr()
{
try
{
Convert.ToInt32("3a3");
return true;
}
catch (Exception)
{
Console.WriteLine("异常");
return false;
}
}
}
class Program
{


static void Main(string[] args)
{
DBHelper dbHelper = new DBHelper();
if (dbHelper.showerr())
{
Console.WriteLine("执行成功!");
}
Console.ReadKey();
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧