多个班级的例外情况 [英] Exceptions throughout multiple classes

查看:55
本文介绍了多个班级的例外情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这篇文章的标题可能听起来有点奇怪,但我还是想知道




我有一个类libray,比方说包含4个类:A,B,C,D。A类

以某种方式引用B,B引用C和C到D.

如果在D类中发生异常,我希望A类得到一个

通知(所有在B到D类的执行)应该终止)。

我想知道如何做到这一点。以下似乎是一个坏主意:


class D {

{

try(...)

catch(SomeException ex)

抛出新的SomeException(D中的错误);

}


C级

{

D d =新D();

尝试

{d.doSomething() ; }

catch(SomeException ex)

{throw new SomeException(ex.Message); }

}


并在B中捕获该异常,并将其抛给A.我想你明白了这个想法

:我很确定这是一个坏主意。但是我应该如何构建呢?否则

在B到D类中的错误处理更糟糕。我希望有人可以给我一个好主意。


谢谢!


Razzie

解决方案

Razzie,


如果你想在发生异常时使类无效,那么你

必须使用try / catch块并设置一个标志,表示不能使用

类。然后你会检查那个班级的每个操作

的那个标志。


我会说这有点矫枉过正,而且,你没有设计

你的类在面对这些异常时足够有弹性。


另外,你是否因为说错误而抛出异常?逻辑错误? />
如果是这样,你可能想重新考虑使用异常(逻辑中的错误确实是一个例外情况)并使用某种返回值,

希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Razzie" < RA **** @ quicknet.nl>在消息中写道

news:uo ************** @ TK2MSFTNGP10.phx.gbl ...

大家好,

这篇文章的标题可能听起来有点奇怪,但我还是想知道
以下的内容。

我有一个类libray,包含4个类: A,B,C,D。A类
以某种方式引用B,B引用C,C引用D.
如果D类发生异常,我希望A类得到这个的通知(B类到D类的所有执行都应该终止)。我想知道如何做到这一点。以下似乎是一个糟糕的想法:

D类{
{
尝试(...)
catch(SomeException ex)
抛出新的SomeException(D中的错误);
}

类C
{d / d D =新D();
尝试
{d.doSomething(); }
catch(SomeException ex)
{throw new SomeException(ex.Message);在B中捕获那个例外,把它扔到A.我想你明白了这个想法
:)我很确定这个坏主意。但是我应该如何构建呢?
在B到D类中根本没有错误处理更糟糕。我希望有人能给我一个好主意。

谢谢!

Razzie



< blockquote> Hello Nicholas,


有些类使用所谓的模板文件来读取数据。例外

可能在模板文件丢失,数据无效等情况下发生。

应该永远不会发生,但如果发生,执行就无法继续。

我真的很想知道抓住并重新抛出一个异常非常糟糕

性能方面(我猜它是因为捕捉成本表现)所以我

don'我不想这样。

我一直在考虑自己返回一些值,比如返回null

而不是一个对象并检查对象是否是null --->必须有的东西

出错了。但是我必须在D级到A级之间使用它,而且我想知道是否只有一种快速方式。

Isn''有可能为此使用事件吗?如果发生D中的异常,则会在A中引发

事件...?


谢谢,


Razzie


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:Oc ************** @ TK2MSFTNGP11.phx.gbl ...

Razzie,
如果你想在发生异常时使类无效,那么你将不得不使用try / catch块并设置一个标志,表明该类不能被使用。然后你会检查那个班级上每个
操作的那个标志。

我会说这有点矫枉过正,而且,你并没有设计你的课程有弹性面对这些例外,还有足够的内容。

另外,你是否因为说错误而抛出异常?逻辑错误?
如果是这样,你可能想重新考虑使用异常(是错误的)在
逻辑真的是一个特殊情况)并使用某种回报价值,
希望这会有所帮助。

-
- Nicholas Paldino [。 NET / C#MVP]
- mv*@spam.guard.caspershouse.com
Razzie < RA **** @ quicknet.nl>在消息中写道
新闻:uo ************** @ TK2MSFTNGP10.phx.gbl ...

大家好,
以下的内容。

我有一个类libray,包含4个类:A,B, C,D。A级
以某种方式引用B,B引用C,C引用D.
如果D类发生异常,我希望A类得到一个
通知(B类到D类的所有执行都应该终止)。我想知道如何做到这一点。以下似乎是一个坏主意:

D类{
{
尝试(...)
catch(SomeException ex)
抛出新的SomeException(D中的错误);
}

类C
{d / d D =新D();
尝试
{d.doSomething(); }
catch(SomeException ex)
{throw new SomeException(ex.Message); }


并在B中捕获该异常,并将其抛给A.我想你得到了这个想法:)我很确定这是一个坏主意。但是我应该如何构建呢?在B到D类中根本没有错误处理更糟糕。我希望有人可以给我一个好主意。

谢谢!

Razzie




Razzie,


您可以使用事件,但这似乎也很多工作。


如果D使用C和C使用B而B使用A,那么为什么不让

对D进行调用的过程结束?我的意思是,为什么不放弃这个过程,然后

重新开始,抛弃链条?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Razzie" < RA **** @ quicknet.nl>在消息中写道

news:eG ************** @ TK2MSFTNGP15.phx.gbl ...

Hello Nicholas,

有些类使用所谓的模板文件来读取数据。当模板文件丢失,数据无效等等时可能会出现异常。
应该永远不会发生,但如果发生,执行就无法继续。
我真的想知道是否抓住并重新抛出一个异常非常糟糕
性能方面(我猜它是,因为捕捉成本表现)所以我不想那样走。
我一直在考虑回归有些人重视自己,比如返回null
而不是一个对象并检查对象是否为null --->必须出错的东西。但是我必须在D级到A级之间使用它,而且我想知道是否只有一种快速的方式。
是不是可以使用事件来做这个?如果发生D中的异常,则会在A中引发
事件?

谢谢,

Razzie

" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>在消息新闻中写道
:Oc ************** @ TK2MSFTNGP11.phx.gbl ...

Razzie,
如果要在发生异常时使类无效,那么您将必须使用try / catch块并设置一个标志,指示该类不能使用。然后你会检查那个班级上每个
操作的那个标志。

我会说这有点矫枉过正,而且,你并没有设计你的课程有弹性面对这些例外,还有足够的内容。

另外,你是否因为说错误而抛出异常?逻辑错误?
如果是这样,你可能想重新考虑使用异常(是错误的)在
逻辑真的是一个特殊情况)并使用某种回报价值,
希望这会有所帮助。

-
- Nicholas Paldino [。 NET / C#MVP]
- mv*@spam.guard.caspershouse.com
Razzie < RA **** @ quicknet.nl>在消息中写道
新闻:uo ************** @ TK2MSFTNGP10.phx.gbl ...

大家好,
以下的内容。

我有一个类libray,包含4个类:A,B, C,D。A级
以某种方式引用B,B引用C,C引用D.
如果D类发生异常,我希望A类得到一个
通知(B类到D类的所有执行都应该终止)。我想知道如何做到这一点。以下似乎是一个坏主意:

D类{
{
尝试(...)
catch(SomeException ex)
抛出新的SomeException(D中的错误);
}

类C
{d / d D =新D();
尝试
{d.doSomething(); }
catch(SomeException ex)
{throw new SomeException(ex.Message); }


并在B中捕获该异常,并将其抛给A.我想你得到了这个想法:)我很确定这是一个坏主意。但是我应该如何构建呢?在B到D类中根本没有错误处理更糟糕。我希望有人可以给我一个好主意。

谢谢!

Razzie





Hi all,

The title of this post may sound a bit weird, but I was wondering about the
following nonetheless.

I have a class libray containing, say, 4 classes: A, B, C, D. Class A
somehow has a reference to B, B has a reference to C, and C to D.
If an exception happens in class D, I would like class A to get a
notification of this (all execution on classes B to D should be terminated).
I am wondering how to do this. The following seems like a bad idea:

class D {
{
try(...)
catch(SomeException ex)
throw new SomeException("Error in D");
}

class C
{
D d = new D();
try
{ d.doSomething(); }
catch(SomeException ex)
{ throw new SomeException(ex.Message); }
}

and catch THAT exception in B, and throw it to A. I think you get the idea
:) I''m quite sure this is a bad idea. But how should I structure it then? No
error handling at all in classes B to D is even worse. I hope someone can
give me a good idea.

Thanks!

Razzie

解决方案

Razzie,

If you want to invalidate the classes when an exception occurs, then you
will have to use the try/catch block and set a flag indicating that the
class can not be used. You would then check that flag for every operation
on that class.

I would say that this is a bit overkill, plus, you are not designing
your classes to be resilient enough in the face of these exceptions.

Also, are you throwing exceptions as a result of say, errors in logic?
If so, you might want to reconsider using exceptions (is the error in logic
truly an exceptional case) and use some sort of return value,

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Razzie" <ra****@quicknet.nl> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...

Hi all,

The title of this post may sound a bit weird, but I was wondering about
the following nonetheless.

I have a class libray containing, say, 4 classes: A, B, C, D. Class A
somehow has a reference to B, B has a reference to C, and C to D.
If an exception happens in class D, I would like class A to get a
notification of this (all execution on classes B to D should be
terminated). I am wondering how to do this. The following seems like a bad
idea:

class D {
{
try(...)
catch(SomeException ex)
throw new SomeException("Error in D");
}

class C
{
D d = new D();
try
{ d.doSomething(); }
catch(SomeException ex)
{ throw new SomeException(ex.Message); }
}

and catch THAT exception in B, and throw it to A. I think you get the idea
:) I''m quite sure this is a bad idea. But how should I structure it then?
No error handling at all in classes B to D is even worse. I hope someone
can give me a good idea.

Thanks!

Razzie



Hello Nicholas,

Some classes use so-called template files to read data from. Exceptions
could occur when a template file is missing, has invalid data, etc. That
should never happen but IF it happens, execution cannot continue.
I was really wondering if catching and rethrowing an exception is very bad
performance-wise (I guess it is, since catching costs performance) so I
don''t want to go that way.
I''ve been thinking of returning some value myself, for example return null
instead of an object and check if an object is null ---> something must have
gone wrong. But I''d have to use THAT throughout class D to A, and I was
wondering if there was just one ''fast'' way.
Isn''t it possible to use events for this? If an exception in D happens, an
event is raised in A...?

Thanks,

Razzie

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oc**************@TK2MSFTNGP11.phx.gbl...

Razzie,

If you want to invalidate the classes when an exception occurs, then
you will have to use the try/catch block and set a flag indicating that
the class can not be used. You would then check that flag for every
operation on that class.

I would say that this is a bit overkill, plus, you are not designing
your classes to be resilient enough in the face of these exceptions.

Also, are you throwing exceptions as a result of say, errors in logic?
If so, you might want to reconsider using exceptions (is the error in
logic truly an exceptional case) and use some sort of return value,

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Razzie" <ra****@quicknet.nl> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...

Hi all,

The title of this post may sound a bit weird, but I was wondering about
the following nonetheless.

I have a class libray containing, say, 4 classes: A, B, C, D. Class A
somehow has a reference to B, B has a reference to C, and C to D.
If an exception happens in class D, I would like class A to get a
notification of this (all execution on classes B to D should be
terminated). I am wondering how to do this. The following seems like a
bad idea:

class D {
{
try(...)
catch(SomeException ex)
throw new SomeException("Error in D");
}

class C
{
D d = new D();
try
{ d.doSomething(); }
catch(SomeException ex)
{ throw new SomeException(ex.Message); }
}

and catch THAT exception in B, and throw it to A. I think you get the
idea :) I''m quite sure this is a bad idea. But how should I structure it
then? No error handling at all in classes B to D is even worse. I hope
someone can give me a good idea.

Thanks!

Razzie




Razzie,

You could use events, but that seems like a lot of work as well.

If D uses C and C uses B and B uses A, why not have the process that
makes calls on D just end? I mean, why not just ditch that process and then
start over, discarding the chain?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Razzie" <ra****@quicknet.nl> wrote in message
news:eG**************@TK2MSFTNGP15.phx.gbl...

Hello Nicholas,

Some classes use so-called template files to read data from. Exceptions
could occur when a template file is missing, has invalid data, etc. That
should never happen but IF it happens, execution cannot continue.
I was really wondering if catching and rethrowing an exception is very bad
performance-wise (I guess it is, since catching costs performance) so I
don''t want to go that way.
I''ve been thinking of returning some value myself, for example return null
instead of an object and check if an object is null ---> something must
have gone wrong. But I''d have to use THAT throughout class D to A, and I
was wondering if there was just one ''fast'' way.
Isn''t it possible to use events for this? If an exception in D happens, an
event is raised in A...?

Thanks,

Razzie

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Oc**************@TK2MSFTNGP11.phx.gbl...

Razzie,

If you want to invalidate the classes when an exception occurs, then
you will have to use the try/catch block and set a flag indicating that
the class can not be used. You would then check that flag for every
operation on that class.

I would say that this is a bit overkill, plus, you are not designing
your classes to be resilient enough in the face of these exceptions.

Also, are you throwing exceptions as a result of say, errors in logic?
If so, you might want to reconsider using exceptions (is the error in
logic truly an exceptional case) and use some sort of return value,

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Razzie" <ra****@quicknet.nl> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...

Hi all,

The title of this post may sound a bit weird, but I was wondering about
the following nonetheless.

I have a class libray containing, say, 4 classes: A, B, C, D. Class A
somehow has a reference to B, B has a reference to C, and C to D.
If an exception happens in class D, I would like class A to get a
notification of this (all execution on classes B to D should be
terminated). I am wondering how to do this. The following seems like a
bad idea:

class D {
{
try(...)
catch(SomeException ex)
throw new SomeException("Error in D");
}

class C
{
D d = new D();
try
{ d.doSomething(); }
catch(SomeException ex)
{ throw new SomeException(ex.Message); }
}

and catch THAT exception in B, and throw it to A. I think you get the
idea :) I''m quite sure this is a bad idea. But how should I structure it
then? No error handling at all in classes B to D is even worse. I hope
someone can give me a good idea.

Thanks!

Razzie





这篇关于多个班级的例外情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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