这样写为什么捕捉不到异常?

feilong1314521 2003-07-25 05:27:57
using System;

namespace excep
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MyException:System.Exception
{
public MyException(string Message):base(Message)
{
Console.WriteLine("Error");
}
}

class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
try
{
int i;
i=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}",1000/i);
Console.ReadLine();
}
catch(MyException e)
{
Console.WriteLine("{0}",e.Message);
Console.ReadLine();
}
}
}
}
...全文
61 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
LesterYu 2003-07-25
  • 打赏
  • 举报
回复
未对类进行对象声明。
xixigongzhu 2003-07-25
  • 打赏
  • 举报
回复
-->自定义的异常,必须抛出才可以,是吗?
不抛出的话定义那个异常干什么?!定义它就是为了用它(扔它)。

-->只有系统的异常才可以用CATCH是吗?
没有这种说法,也没有这种规定,只要是异常都能catch,但必须用try...catch...语句来catch.
cppTrier 2003-07-25
  • 打赏
  • 举报
回复
自定义的异常,必须自己throw,才可以被catch到。而系统预定义的异常不需要显式的throw,会自动抛出的。

很多书上说C#的异常,不可以throw,其实是错的,只有系统预定义的异常才不需要throw,别被误导了。
feilong1314521 2003-07-25
  • 打赏
  • 举报
回复
自定义的异常,必须抛出才可以,是吗?
只有系统的异常才可以用CATCH是吗?
xixigongzhu 2003-07-25
  • 打赏
  • 举报
回复
你自己扔出异常肯定有调试框呀,如果你不想要就try掉。
xixigongzhu 2003-07-25
  • 打赏
  • 举报
回复
using System;

namespace excep
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MyException:System.Exception
{
public MyException(string message):base(message)
{
Console.WriteLine("{0}",Message);
}
}

class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.WriteLine("input a number:");
int i=Convert.ToInt32(Console.ReadLine());
if (i>10)
{
Console.WriteLine("OK");
Console.ReadLine();
}
else
{
throw (new MyException("Error"));
}
}catch(Exception e){
Console.WriteLine(e);
}
}
}
}
ruihuahan 2003-07-25
  • 打赏
  • 举报
回复
try块中的代码没有任何一行throw你定义的异常。
feilong1314521 2003-07-25
  • 打赏
  • 举报
回复
如何才能不让它蹦出那个调试框呢
雪狼1234567 2003-07-25
  • 打赏
  • 举报
回复
语句如下:
if(value>0)
{
}
else
{ throw new ArgumentOurOfRangeException("you divide the 0")
}
feilong1314521 2003-07-25
  • 打赏
  • 举报
回复
using System;

namespace excep
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class MyException:System.Exception
{
public MyException(string message):base(message)
{
Console.WriteLine("{0}",Message);
}
}

class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.WriteLine("input a number:");
int i=Convert.ToInt32(Console.ReadLine());
if (i>10)
{
Console.WriteLine("OK");
Console.ReadLine();
}
else
{
throw (new MyException("Error"));
}
}
}
}

我现在这样写,可以捕捉到了,可是为什么还会蹦出调试的框框来啊
雪狼1234567 2003-07-25
  • 打赏
  • 举报
回复
把MyException 换成System.Exception,如果你定义了自己的Exception.要使用语句throw抛出来
xixigongzhu 2003-07-25
  • 打赏
  • 举报
回复
因为这段:
int i;
i=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}",1000/i);
Console.ReadLine();
都是系统的API,系统的API不可能扔出你定义的异常,都是系统API定义的异常,所以抓不到。你如果要抓的话,只能用系统定义的并且这段可能扔出的异常来抓(如Exception)。

111,098

社区成员

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

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

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