我可以在c#.net中定义自己的异常类型吗?如果是的话,怎么......?我希望这似乎是一个不规则的问题,但仍然想知道? [英] can i define my own exception types in c#.net? if yes, how..?i hope this might seems like a irregular question but still wants to know ?

查看:75
本文介绍了我可以在c#.net中定义自己的异常类型吗?如果是的话,怎么......?我希望这似乎是一个不规则的问题,但仍然想知道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义我自己的异常类型。



//考虑我在代码后面或BL或DA层有我自己的自定义方法..



public void my_first_method()

{

//一些自定义代码执行..

//可能会抛出一些错误..

}

所以..如果没有错的话...

我可以在我的东西事件处理程序如..



试试

{



my_first_method() ;

my_second_method();

my_third_method();



}

catch(my_first_methodException fex)

{



}

catch(my_second_methodException sex)

{



}

catch(my_third_methodException tex)

{



}

catch(exception ex)

{

//如果不属于3以上的例外来这里..

}



我不确定这是一个正常的问题,我只是试图找出是否有可能这样做。只是一个想法我在周围漫游..请指教,提前谢谢..

is it possible to define my own exception type.

//consider i have my own custom method in code behind or BL or DA layer..

public void my_first_method()
{
// some custom code execution..
//might throw some errors..
}
so.. if am not wrong..
can i have something in my event handler like..

try
{

my_first_method();
my_second_method();
my_third_method();

}
catch(my_first_methodException fex)
{

}
catch(my_second_methodException sex)
{

}
catch(my_third_methodException tex)
{

}
catch(Exception ex)
{
//if doesn't belongs to above 3 exception come here..
}

am not sure whether this is a normal question , i was just trying to findout is it possible to do so..just a thought i was roaming around..please advise, thanks in advance..

推荐答案

是的,它非常简单:



Yes, its quite easy:

public class MyExceptionClass : Exception
{
   //Put the exception properties and constructors here, its good
   //to have the same or more constructors as the base Exception.
}





然后就像正常一样使用它:





Then just use it like normal:

throw new MyExceptionClass();


这篇关于我可以在c#.net中定义自己的异常类型吗?如果是的话,怎么......?我希望这似乎是一个不规则的问题,但仍然想知道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆