-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
except:
should map to the more conservativeexcept CatchableError:
instead of except Exception:
#89
Comments
I agree completely with this proposal and the reasoning behind it. |
I've implemented this, there are a few problems with some specific cases (beside the endless and boring churn work needed to adjust all the tests). Foreign (js/cpp) exceptions cannot be caught anymore, let me explain this point with an excerpt from the test suite: import json
try:
discard parseJson"""{ invalid"""
except:
echo "CAUGHT" Right now the output is Moreover I noticed another problem, if a foreign (not a Exception subclass) is caught in the default exception handler all you get is a lousy segfault since it is treated as if it were a Nim one with all its fields and stuff. Oh, and one of the Nim in Action examples has to be modified ( |
A solution to foreign exceptions/exceptions that dont extend one of CatchableError or Defect is the following: try:
something()
except Defect:
raise
except:
handleException() I think this is how it should be regardless, the system module can't categorize every single exception type in existence. CatchableError and Defect should be convenience options, not rule with an iron fist. As for whether or not the compiler can automate this, Defect could have a |
With |
with proposal:
|
I agree with @Araq it can be closed. try:
something()
except Exception:
handle all Nim Exceptions
except:
handleNonNimExceptions() Comments about getCurrentException() are irrelevant. It can be used outside of try/except blocks and there are cases where it is required outside of try/except blocks. |
my point about try: bar()
except Foo: # user is unaware this is a foreign exception
let e = getCurrentException()
echo e.msg # SIGSEGV so that case could be made illegal at CT; however it wouldn't prevent other cases where try: bar()
except Foo: # user is unaware this is a foreign exception
fun()
proc fun() =
let e = getCurrentException()
echo e.msg # SIGSEGV so maybe it's not worth the special case Anyway, closing, it's sane enough currently. |
What is the rationale for rejecting this proposal? The reason why But since I've personally fixed more than 20 places in our codebase and third party libraries when |
re-opening since there's no consensus yet. @zah what do you think of try: bar
except: # means `except CatchableError:`; preferred concise syntax is mapped to the safe, sane option
except AsstionDefect: # useful in some cases, eg test frameworks / servers that can't die / some repl implementations
except MyCppException: # 1 particular foreign exception
except {.foreign.}: # catch-all for foreign exceptions
except {.all.}: # catch-all, both nim Exception and foreign exception |
We can perhaps work-around the problem of foreign exceptions with some magical types such as One could argue that in JavaScript mode (and C++), the default could be |
that could work too, not really sure it's simpler (implementation wise or usage wise) than the simple pragma not that, unlike for C++, the following works for js: try {
} catch (e) { // catches all exceptions AND have a reference to it via `e`
} whereas in C++ the best catch-all you can do is: catch(...) // no reference `e` possible so but I'd prefer to use when defined(js):
type ForeignException = JsObject
elif defined(cpp):
type ForeignException {.magic: "ForeignException".} = object
# this type is special in C++ mode; eg can't use sizeof(ForeignException) etc |
ok, can't just |
Isn't this proposal a little unintuitive? I can't think of a programmer that wouldn't expect |
Now catching bare exceptions gives a warning now. It can be closed. |
follow up for nim-lang/Nim#8237, and with the same principles ("Technically, assertion failures are not catchable."),
except:
should map to the more conservativeexcept CatchableError:
instead ofexcept Exception:
User can always explicitly set
except Exception:
(or evenexcept Defect:
depending on his use case) when he needs, eg in unittests.note
except:
but now that @Araq implementedCatchableError
as root of all ethically catchable exceptions, it makes sense to keepexcept:
, and give it the meaning ofexcept CatchableError:
; it's a sensible default.The text was updated successfully, but these errors were encountered: