简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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