无法抛出异常

locustella 2010-10-21 09:35:37
我使用的是vs2010,程序源代码如下,是课本上一个非常简单的抛出DivideByZeroException异常的程序……原本应该是在控制台窗口显示,最后还是弹出了错误窗口,请问究竟怎么回事呢?
“An unhandled exception of type 'System.Exception' occurred in ConsoleApplication2.exe
Additional information: 除法运算发生了错误~
Attempted to divide by zero.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static int division(int x, int y)
{
try
{
return (int)(x / y);
}

catch (DivideByZeroException ex) { throw new Exception("除法运算发生了错误~" + Environment.NewLine + ex.Message); }
catch (Exception ex) { throw new Exception("除法运算发生了错误!!!!!!"); }
}
static void Main(string[] args)
{
int i = division(100, 0);

Console.ReadLine();
}
}
}

恳求众高手回答~谢谢!
...全文
234 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozhongxian001 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 oo_p_c 的回复:]
要想在控制台上输出很简单,你把 throw new Exception改成Console.WriteLine()就可以了
别忘了 mark.呵呵
[/Quote]
七楼,九楼回答的很对楼主可以采纳下。
一般捕获异常不需要搞这么多catch语句。这个捕获所有异常
catch(System.Exception ex)
{
Message.Show(ex.ToString());
}
51Crack 2010-10-21
  • 打赏
  • 举报
回复
你抛给了Main,但Main中没处理异常!
lrh_079 2010-10-21
  • 打赏
  • 举报
回复
在division方法里抛出异常,而在它的上级方法main里又没有捕获异常,自然是要弹出异常窗口的
jieon 2010-10-21
  • 打赏
  • 举报
回复
throw new Exception 换成Console.WriteLine
shadan05 2010-10-21
  • 打赏
  • 举报
回复
控制台Console.WriteLine();用这个输出错误,就不会弹出了。


deyygywxf 2010-10-21
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
try{
int i = division(100, 0);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.ReadLine();
}
wsk200209 2010-10-21
  • 打赏
  • 举报
回复
学习!
qqrto 2010-10-21
  • 打赏
  • 举报
回复
顺便问一下楼主的结贴率怎么这么高?????都110%了
qqrto 2010-10-21
  • 打赏
  • 举报
回复
楼主想要显示的话应该这样做啊:
static void Main(string[] args)
{
try{
int i = division(100, 0);
}catch(Exception ex){
Console.WriteLine(ex.Message);
}
}
}
pigadd 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 oo_p_c 的回复:]

要想在控制台上输出很简单,你把 throw new Exception改成Console.WriteLine()就可以了
别忘了 mark.呵呵
[/Quote]
+1
就是这个
OO_P_C 2010-10-21
  • 打赏
  • 举报
回复
要想在控制台上输出很简单,你把 throw new Exception改成Console.WriteLine()就可以了
别忘了 mark.呵呵
OO_P_C 2010-10-21
  • 打赏
  • 举报
回复
你判断一下,被除数不等于零就不会报错了.
周药师 2010-10-21
  • 打赏
  • 举报
回复

//捕获异常搞的那么麻烦干嘛 直接用
try
{
//your code
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
inttlegence 2010-10-21
  • 打赏
  • 举报
回复
“An unhandled exception of type 'System.Exception' occurred in ConsoleApplication2.exe
Additional information: 除法运算发生了错误~
Attempted to divide by zero.

这个错误就是你throw出来的,想在窗口显示,你用console输出错误
inttlegence 2010-10-21
  • 打赏
  • 举报
回复
你抛出了异常,当然弹出错误窗口了

服了
grasshopperwarbler 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yxy10072510329 的回复:]
一个try怎么会对应两个catch?
[/Quote]

一个try应该可以对应多个catch的吧?
yxy10072510329 2010-10-21
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
try{
int i = division(100, 0);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

Console.ReadLine();
}
yxy10072510329 2010-10-21
  • 打赏
  • 举报
回复
一个try怎么会对应两个catch?

111,129

社区成员

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

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

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