ArchUnit icon indicating copy to clipboard operation
ArchUnit copied to clipboard

add support for dependencies from caught exceptions

Open bannmann opened this issue 1 month ago • 2 comments

Referencing an exception class in the catch clause of a try-catch block can certainly be considered a dependency on that class, similar to a throws clause or an instanceof check: if the referenced class is renamed or deleted, the dependent code will break.

To prevent this from happening, one could write architecture rules that e.g. limit the use of a certain third-party library to a specific layer, or allow-list only certain "stable" or "vetted" classes.

Unfortunately, such rules will not notice when exception classes are referenced in catch clauses of try-catch blocks. The reason is that so far, ArchUnit does not generate a Dependency for them (it only does that once a member of the exception class is used, e.g. calling a method). This behavior defeats the purpose of implementing such a rule in the first place.

Thus, we change ArchUnit so that it considers each exception type referenced in catch clauses of try-catch blocks to be a dependency on that type. As the information is already available in the model via TryCatchBlock.caughtThrowables, all it takes is wiring it up to creation and handling of Dependency instances.

Resolves: #1232

bannmann avatar Nov 24 '25 22:11 bannmann

Glad you like it!

I pushed the new changes as a separate commit; tell me if I should squash everything before you merge.

bannmann avatar Nov 26 '25 14:11 bannmann

I totally forgot about the "rebase" part last time; have now done this.

Let me know if there's anything else I should do!

bannmann avatar Dec 04 '25 18:12 bannmann