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

笑着哭i 提交于 2019-12-11 10:32:30

问题


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?


回答1:


Probably like this:

Note that <<throws>> isn't a standard UML stereotype. 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. (See here for list of supported stereotypes as at UML 2.1).

You could model MyClass-MyException as a simple binary association rather than a dependency but it doesn't really hold semantically; 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. Case in point: pedantically, MyClass doesn't throw any exceptions, one or more methods of MyClass throws the 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.



来源:https://stackoverflow.com/questions/8074664/how-can-i-model-a-custom-exception-association-in-a-class-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!