B7 Informatik 2 Summer 2025

Website of Prof. Dr. Barne Kleinen, Professor for Media Informatics (Bachelor/Master) at HTW Berlin

Exceptions

     <prev

Sequence of methods calling methods calling other methods….

sequenceDiagram
    A->>+B: message_call()
    B->>+C: message_call()
    C->>+D: message_call()
    D->>+E: message_call()
    E->>+F: message_call()
    F->>+G: message_call()
    G->>+H: message_call()
    H->>+I: message_call()
    I-->>-H: return
    H-->>-G: return
    G-->>-F: return
    F-->>-E: return
    E-->>-D: return
    D-->>-C: return
    C-->>-B: return
    B-->>-A: return

Something goes wrong

very deep down the call sequence…

sequenceDiagram
    A->>+B: message_call()
    B->>+C: message_call()
    C->>+D: message_call()
    D->>+E: message_call()
    E->>+F: message_call()
    F->>+G: message_call()
    G->>+H: message_call()
    H->>+I: message_call()
    I-->>-H: return 
    Note right of I: Something goes wrong!!
    H-->>-G: return
    G-->>-F: return
    F-->>-E: return
    E-->>-D: return
    D-->>-C: return
    C-->>-B: return
    B-->>-A: return

Where it could be fixed/handled

sequenceDiagram
    A->>+B: message_call()
    B->>+C: message_call()
    C->>+D: message_call()
    D->>+E: message_call()
    
    E->>+F: message_call()
    F->>+G: message_call()
    
    G->>+H: message_call()
    H->>+I: message_call()
    Note right of I: Something goes
WRONG!! I-->>-H: return H-->>-G: return G-->>-F: return Note right of E: <- This method can
retry in a loop F-->>-E: return E-->>-D: return D-->>-C: return Note right of B: <- This method
can tell/ask user C-->>-B: return B-->>-A: return

But…. how to tell the methods / propagate the error??

Manual Error Propagation?

Requires adding error handling / Propagation logic to all methods in between where the error occurred until it can be handled:

sequenceDiagram
    A->>+B: message_call()
    B->>+C: message_call()
    C->>+D: message_call()
    D->>+E: message_call()
    
    E->>+F: message_call()
    F->>+G: message_call()
    
    G->>+H: message_call()
    H->>+I: message_call()
    Note right of I: Something goes
WRONG!! break error I-->H: propagate error end break error H-->G: propagate error end break error G-->>F: propagate error end break error F-->>E: propagate error end Note right of E: <<== This method can
retry in a loop I-->>-H: return H-->>-G: return G-->>-F: return Note right of E: Retry did not help :-( break error E-->>D: propagate error end break error D-->>C: propagate error end break error C-->>B: propagate error end F-->>-E: return E-->>-D: return D-->>-C: return Note right of B:<<== This method
can tell/ask user C-->>-B: return B-->>-A: return

With Exceptions

  • if Exception A is thrown, control jumps to next enclosing catch A block up the call stack
  • all methods inbetween have to either
    • do nothing if the Exception is Unchecked
    • declare throws A in their method header if the Exception is Checked
  • see
sequenceDiagram
    A->>+B: message_call()
    B->>+C: message_call()
    C->>+D: message_call()
    D->>+E: message_call()
    
    E->>+F: message_call()
    F->>+G: message_call()
    
    G->>+H: message_call()
    H->>+I: message_call()
    Note right of I: Something goes
WRONG!!
Throw Exception A Note right of E: <<== This method can
retry in a loop break Exception A I-->>E: control jumps to next catch A end Note right of E: <<== catch(Exception A){ retry in loop } Note right of E: Retry did not help - re-throw A Note right of B:<<== This method
can tell/ask user break Exception A E-->>B: control jumps to next catch A end Note right of B:<<== catch(Exception A)
{ tell/ask user } I-->>-H: return H-->>-G: return G-->>-F: return F-->>-E: return E-->>-D: return D-->>-C: return C-->>-B: return B-->>-A: return