多个班级的例外情况

多个班级的例外情况

本文介绍了多个班级的例外情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 这篇文章的标题可能听起来有点奇怪,但我还是想知道 。 我有一个类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 解决方案 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 ... Hi all,The title of this post may sound a bit weird, but I was wondering about thefollowing nonetheless.I have a class libray containing, say, 4 classes: A, B, C, D. Class Asomehow 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 anotification 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? Noerror handling at all in classes B to D is even worse. I hope someone cangive me a good idea.Thanks!Razzie 解决方案 这篇关于多个班级的例外情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-01 12:27