2

I'm struggling to understand how can I represent following code by UML Sequence diagram:

What I already have:

Picture of my UML sequence diagram

Java code:

public static Connection getDbConnection() throws SQLException{
if (instanceOfDbConnect == null) {
        instanceOfDbConnect = new DbConnection();
        System.out.println(" Connection  - - - - - - - -  Trying to create DBConnection.");
    }
    try {
        return DriverManager.getConnection(URL,user,password);
    } catch (SQLException e) {
        throw e;
    }

}

How can I deal with this throwing SQLException if it's done in this way? It's seems to me that I should get a return message (throw e) from SQLException to DbConnection. But what is the sender message from DbConnection to SQLException?

Thank you very much for all your help!

bruno
  • 32,421
  • 7
  • 25
  • 37
Banik
  • 320
  • 4
  • 9
  • 3
    I think that if you are trying to represent your design to that level of detail in UML then you are probably wasting your time, and your bosses time. UML is a high level design tool. But this is coding, not design. The code itself is a much better representation ... for anyone who needs to understand what it is doing / should be doing. – Stephen C May 21 '19 at 10:23
  • @StephenC I disagree with you, UML is not limited to high level design ( I am probably quite well placed to know that distributing an UML tool producing source code ;-) ) – bruno May 21 '19 at 10:52
  • 1
    OK. I was actually peripherally involved in the OMG meta-modelling of UML, back in the late 1990's. I'm pretty damn sure that it was never the designers in intention that UML be used for stuff like this. Now clearly, you can do it ... just like you can use a sledge hammer to crack peanuts ... put that doesn't make it a *productive* use of your time as a programmer. – Stephen C May 21 '19 at 11:01
  • 2
    You almost did it. Think of exceptions as an`alt` fragment. Like said above: use UML for what it's good at and don't get into "graphical programming". – qwerty_so May 21 '19 at 12:59

1 Answers1

2

As commented UML is not the best when it comes to real code. Your eight lines of code (not counting the single closing bracket lines) are very clear. An attempt to draw that as SD could look like

enter image description here

Would that help in documenting? Well, it depends. You have to deal with lots of graphical elements and depending on the tool you spend a lot of time arranging arrows frames and life lines. That might turn out being a PITA. And it's even not clearer than this little code example.

Now consider this example: enter image description here

Here the behavior is stored in the notes of the messages (using Enterprise Architect). Instead one could use a note element and place it on the diagram.

So what ever route you go: it's all about communication. Use what ever is best to communicate the idea. SDs are great when you want to show a complex collaborations where many objects are involved. But at a certain level teh codez are just the best to transport the message.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86