简体   繁体   English

如何在类图中为自定义异常关联建模?

[英]How can I model a custom exception association in a class diagram?

Reading here , it seems modelling a custom exception class using a generalisation is common place. 在这里阅读,使用通用化对自定义异常类进行建模似乎很常见。 What it doesn't mention is how I can model an association with a class that could potentially throw the custom exception. 它没有提到的是我如何使用可能引发自定义异常的类对关联进行建模。 Note, I'm not asking how to model the sequence behaviour when it comes to raising the exception; 注意,在引发异常时,我并不是问如何对序列行为建模; I'm specifically wanting to model the association. 我特别想对关联进行建模。 Or is this a misuse of the class diagram? 还是这是对类图的滥用?

Probably like this: 大概是这样的:

在此处输入图片说明

Note that <<throws>> isn't a standard UML stereotype. 请注意, <<throws>>不是标准的UML构造型。 There's nothing wrong with that - it's perfectly fine to define your own. 这没什么不对-定义自己的名字完全可以。 If you want to stick to standards though, <<create>> is probably the closest. 如果您想遵守标准,那么<<create>>可能是最接近的标准。 (See here for list of supported stereotypes as at UML 2.1). (请参阅此处 ,以获取UML 2.1支持的构造型列表)。

You could model MyClass-MyException as a simple binary association rather than a dependency but it doesn't really hold semantically; 可以将 MyClass-MyException建模为简单的二进制关联而不是依赖项,但是它实际上并不具有语义上的意义; there's no systematic relationship among them. 它们之间没有系统的关系。 Similar to the case where a Factory creates instances. 与工厂创建实例的情况类似。 There's a good article on Dependency relationships here if you need more info. 如果您需要更多信息, 这里有一篇关于依赖关系的好文章。

is this a misuse of the class diagram? 这是对类图的滥用吗?

Not if you find it useful. 如果您觉得它有用,那不是。 UML is a tool: use it where & how it helps, ignore it where it doesn't. UML是一种工具:在哪里使用它以及如何使用它,在没有使用它的地方忽略它。 Case in point: pedantically, MyClass doesn't throw any exceptions, one or more methods of MyClass throws the Exception. 恰当的例子:从理论上讲, MyClass不会引发任何异常, MyClass一个或多个方法会引发Exception。 So the dependency is an abstraction. 因此,依赖关系是一种抽象。 However as long as: 但是只要:

  • you - and whomever else needs to read the model - understands that, and 您-还有其他需要阅读模型的人-会理解的,并且
  • it's useful 这很有用

then it's not misuse, it's use. 那不是滥用,而是使用。

hth. hth。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM