问题
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