将已检查的异常添加到C# [英] Adding Checked Exceptions to C#

查看:65
本文介绍了将已检查的异常添加到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨晚喝了太多咖啡,并提出了一个关于

如何

的建议,将检查过的例外添加到C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm


预期评论< g>


问候,

杰夫


***通过开发人员指南 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

Jeff ,


异常处理似乎每个月至少出现一次。这可能是另外一个长线程。


我同意在尝试执行某个操作之前检查条件(

现实可行)但我几乎不需要一个

异常类。所有我做过例外的事情都是向

用户显示一条消息(取决于应用程序的类型)并记录它。如果我需要在代码中使用

不同的原因,那么我先检查一下条件,

这种情况​​我通常不会在包装的泛型类中执行此操作

常用功能。

Jeff,

Exception handling seems to crop up at least once a month. This is probably
going to be another one of those long threads.

I agree with checking conditions before trying to perform an action (where
realistically feasible) but I have little need for anymore than one
Exception class. All I ever do with an exception is display a message to the
user (depending on the type of application) and log it. If I need to take
different cause within code then I check for the condition before hand, in
which case I cannot normally do this in a generic class which wraps up
common functionality.

if(!File.Exists(file))
{
抛出新的FileNotFoundException(filePath) ); $
}


您的示例代码检查文件是否存在但只是抛出异常,

这与框架的不同之处?是的,它在开放之前执行收盘
但是我更愿意在主要调用

代码中明确地执行此操作。对于我试图打开一个关闭的文件应该抛出异常而不是隐藏

我的代码中的错误我没有这样做

提出了一个关于如何添加的建议检查C#的例外情况


无论如何,您的提案实际上并不属于

" checked"类别例外。我似乎记得在
www.CodeProject.com 上看到了一些人通过使用属性。


但也许我错过了你的观点...


Phil ...


" Jeff Louie" < JE ******** @ yahoo.com>在消息中写道

新闻:eI ************** @ TK2MSFTNGP11.phx.gbl ...我昨晚喝了太多咖啡并想出了一个建议关于如何将已检查的例外添加到C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

预期评论< g>
问候,
Jeff

***通过开发人员发送的指南 http: //www.developersdex.com ***
不要只是参加USENET ......获得奖励!
if (!File.Exists(file))
{
throw new FileNotFoundException(filePath);
}
Your example code checks if the file exists but simply throws an exception,
how does this differ to what the framework does? Yes it performs a close
before an open but I prefer to do this explicitly within the main calling
code. For me trying to open a closed file should throw an exception not hide
the bug in my code where I failed to do so
came up with a suggestion about how to add checked exceptions to C#
In any case your proposal does not really fall into the category of
"checked" exceptions. I seem to remember seeing something in
www.CodeProject.com where somebody achieved this by using Attributes.

But perhaps I have missed your point...

Phil...

"Jeff Louie" <je********@yahoo.com> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl...I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



如果我说得对,你的建议就是为每个班级定义至少4个不同的例外




接口怎么样?


class MyThread:IRunnable

{

public void Run(){}

}


会抛出什么异常?我假设IRunnableException和

MyThreadException。


-

cody


[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk

" Jeff Louie" < JE ******** @ yahoo.com> schrieb im Newsbeitrag

新闻:eI ************** @ TK2MSFTNGP11.phx.gbl ...
If I get it right your proposal is to define at least 4 different exceptions
for each class?

And what about interfaces?

class MyThread : IRunnable
{
public void Run(){}
}

What exceptions will run throw? I assume both IRunnableException AND
MyThreadException.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Jeff Louie" <je********@yahoo.com> schrieb im Newsbeitrag
news:eI**************@TK2MSFTNGP11.phx.gbl...
我喝了太多咖啡昨晚又提出了一个建议,即如何为C添加已检查的异常

http://www.geocities.com/jeff_louie/OOP/oop14.htm

期待的评论< g> ;

问候,
Jeff

***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!





" Jeff Louie" < JE ******** @ yahoo.com>在消息中写道

新闻:eI ************** @ TK2MSFTNGP11.phx.gbl ...

"Jeff Louie" <je********@yahoo.com> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl...
我喝了太多咖啡昨晚又提出了一个建议,即如何为C添加已检查的异常

http://www.geocities.com/jeff_louie/OOP/oop14.htm

期待的评论< g> ;
I drank too much coffee last night and came up with a suggestion about
how
to add checked exceptions to C#

http://www.geocities.com/jeff_louie/OOP/oop14.htm

Comments expected <g>




据我所知,合同中的这个问题会受到同样的问题的影响,我需要定期检查例外情况。


为什么我必须抓住MyClassFileNotFoundException和

YourClassFileNotFoundException只是因为人为的异常

合约?或者更糟糕的是MyClassYourClassFileNotFoundException和

YourClassMyClassFileNotFoundException。


在我看来,你可能最终得到了几十个例外,

意味着同样的事情,但只是有不同的起源点。



As best I can tell, this catch by contract suffers from the same problem I
have with regular checked exceptions.

Why is it that I would have to catch MyClassFileNotFoundException and
YourClassFileNotFoundException simply because of artificial exception
contracts? Or worse MyClassYourClassFileNotFoundException and
YourClassMyClassFileNotFoundException.

It seems to me you may actually end up with several dozen exceptions that
mean the same thing, but just have different points of origin.


这篇关于将已检查的异常添加到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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