[JAVA-4575] Driver code should catch Exception instead of RuntimeException even if no checked exception is declared Created: 13/Apr/22  Updated: 07/Jul/22  Resolved: 07/Jul/22

Status: Closed
Project: Java Driver
Component/s: Error Handling
Affects Version/s: None
Fix Version/s: 4.7.0

Type: Improvement Priority: Unknown
Reporter: Valentin Kavalenka Assignee: Maxim Katcharov
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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)


 Comments   
Comment by Githook User [ 07/Jul/22 ]

Author:

{'name': 'Maxim Katcharov', 'email': 'maxim.katcharov@mongodb.com', 'username': 'katcharov'}

Message: Replace RuntimeException in catch with Exception to support Kotlin (#971)

Kotlin does not support checked exceptions, so our handlers will use Exception. All handlers are replaced, even those obviously unaffected, for the sake of a consistent noticeable pattern.

JAVA-4575
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/79512417f2990c3b795d228ba35bf48a3cfbd9bb

Comment by Githook User [ 13/Apr/22 ]

Author:

{'name': 'Emanuele Sabellico', 'email': 'emasab@gmail.com', 'username': 'emasab'}

Message: Abort transaction in all cases (#881)

See JAVA-4575 for more details.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/ebada8e17c18e9a8574393fd2bdc9c5231290f58

Generated at Thu Feb 08 09:02:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.