Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4575

Driver code should catch Exception instead of RuntimeException even if no checked exception is declared

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • 4.7.0
    • Affects Version/s: None
    • Component/s: Error Handling
    • Labels:
      None

      Due to Kotlin not supporting checked exceptions, it is possible to break expectations we have in Java, and throw a checked exception from a method that does not declare throwing checked exceptions. See the discussion in https://github.com/mongodb/mongo-java-driver/pull/881 for more details. In order to work around this problem, we should do the following:

      1. Treat any method as being able to throw checked exception even it is not declared (despite this not being true for all methods, for simplicity and robustness it is better to treat all of them this way).
      2. Replace all catch blocks that catch RuntimeException s (or subclasses, but those are harder to find) with blocks that catch Exception.
      3. Explicitly look at the known places were the driver may execute user-provided code and add code that wraps checked exceptions in unchecked ones. jeff.yemin@mongodb.com came up with the following list
        1. withTransaction
        2. authenticator-related callbacks
        3. any event listener
        4. Bson.toBsonDocument (in case someone has gone to the trouble to implement the Bson interface)

            Assignee:
            maxim.katcharov@mongodb.com Maxim Katcharov
            Reporter:
            valentin.kovalenko@mongodb.com Valentin Kavalenka
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: