nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.52k stars 1.46k forks source link

Hint on overly broad exception handler #23367

Open arnetheduck opened 7 months ago

arnetheduck commented 7 months ago

Summary

Introduce a hint when exception handler catches a broader exception type than is raised

Description

Similar to #23366 but distinct in semantics, an exception handler that catches more exceptions than necessary may introduce unexpected runtime behavior when dependent code changes:

proc f() {.raises: [ValueError].} = 
  raise (ref ValueError)()

try:
  f()
except CatchableError: # Only ValueError is raised here which is more specific 
  discard

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

Araq commented 7 months ago

That is in conflict with my long term goals of only tracking "any" exception thrown vs "no" exception thrown as opposed to this fine grained sophistry which Nim inherited from other languages and didn't come from first principles.

arnetheduck commented 7 months ago

That is in conflict with my long term goals of only tracking "any" exception

That's not the language we have to work with today though, where this hint becomes a necessary feature to get rid of excessive exception handling (which turns out to be costly for larger projects).

As to the introduction of "any" vs "no" exception, I imagine that can be done with a separate tracker and a separate keyword at which point both will coexist (experimentally) with some overlap time (meaning that the hint is useful today and for the foreseeable future until that happens).

It would also require addressing the fundamental problem with except:, namely that it sometimes catches Defect and sometimes not (it would be workable if it never caught defect, the catching of which would require an explicit except Defect).