您的位置:程序门 -> .net技术 -> c#



自定义异常


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


自定义异常
发表于:2007-01-26 07:56:09 楼主
希望哪位哥哥.可以帮小弟写一个自定义异常.从抛出.一直到捕获.谢谢啦
发表于:2007-04-25 16:06:081楼 得分:0
嗯...寫一個可能沒有時間,不過提供一些思路:

1.寫一個類,繼承自exception或者applicationexception(微軟建議繼承后者):

public   userexception   :   system.applicationexception
{

}

2.
try
{
}  
catch(exception   ex)
{  
      //對異常進行處理;

      throw   new   userexception();   //拋出自定義的異常;
}

发表于:2007-04-25 16:06:522楼 得分:0
上面寫錯了

public   class   userexception   :   system.applicationexception
{

}
发表于:2007-04-25 16:25:263楼 得分:0
throw   new   “what   you   want   other   see”;
发表于:2007-04-25 16:38:314楼 得分:0
using   system;
class   exceptiondemo
{
static   void   main()
{
int   num;
try
{
num   =   convert.toint32(console.readline());
if(num   <   0)
throw   new   myexception(2000,   "小于零 ");
}
catch(myexception   err)
{
console.writeline(err.number);
console.writeline(err.message);
}
}
}
class   myexception:system.applicationexception
{
private   uint   errornumber;
public   myexception():base( "myexception "){}
public   myexception(uint   number,   string   message):base(message)
{
this.errornumber   =   number;
}
public   uint   number
{
get   {return   this.errornumber;}
}
}



快速检索

最新资讯
热门点击