-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
rationalize uses of error() and throw() #5694
Comments
It may be worth looking at Lumberjack.jl, which seems like a very sane approach to logging (though I'm not sold on the cutesy names). I like the approach that As a side note - When I started using the logging module in python instead of scattering my code with prints it was a huge win. Being able to change the log level of the program rather then commenting in and out log messages all the time is something people overlook the value of. Not sure what the criteria are for when things get pulled into Base, but i think that encouraging this sort of logging is a good thing and has basically zero overhead when writing code. |
+1 for |
fwiw, I've put in some effort to systematize the linalg codebase to use |
Another +1 for throw. I also like the idea of pulling a logging framework into Base once the frameworks become more mature. |
I find errors are often hard to classify. For example I just noticed
While most DimensionMismatches arise for two things with different shapes that should be the same, this one is for non-squareness. In still other cases the exact shape doesn't have to match, but the number of elements does. It's not clear if there should be a I'm not sure what the solution is, but there should probably be relatively few exception types so you don't have to think too much about which one to use. |
I'll hazard a guess here. I think the current To carry over an idea from JuliaLang/LinearAlgebra.jl#45, I wanted |
If |
I agree that error messages are hard to classify, and having lots of error types is sometimes just as confusing. But I think your example highlights what I was trying to get at before. If you were consuming the above code and it threw an error, isn't |
Ultimately I agree we will have to bite the bullet and remove |
One approach would be to recommend using very specific exceptions, and make them inherit from more general ones when applicable. For example, Ideally, I think every error message should be generated automatically by combining the exception type and arguments, just like suggested in @jiahao's comment #5694 (comment) above. This could even be enforced by only allowing error messages to be printed via arguments to a specific exception type. |
Is there an issue already for catching specific exception types? A quick search didn't find one. Obviously, we can use (Better non-dispatch-based pattern matching is something I'm still wanting in the core language... cf. https://github.com/kmsquire/Match.jl, #3146, #5410) |
The |
To clarify, the consensus is that any |
Currently all of these do the same thing:
We're not consistent about which of these should be used. It's unnecessarily confusing. Some background:
error
would be for signaling exceptional conditions, andthrow
would just be control flow.error(string)
wraps the string in anErrorException
, so that we generally throw things of typeException
. This is not strictly required though, and one could throw a string directly.throw
is a low-level, non-generic function.error
is thatthrow(SomeException("message"))
is generally too verboseIf we were going to use
throw
in the control-flow sense, it would probably need 2 arguments (throw(label, value)
), so we don't necessarily need 2 names to disambiguate.My preferred idiom is
throw(SomeException())
since it clearly makes an exception object and then throws it. The only downside is verbosity.Wrapping a string in an
ErrorException
obviously doesn't add any interesting information, so maybethrow("message")
should be considered acceptable. Thenerror
could be an alias or deprecation forthrow
.The text was updated successfully, but these errors were encountered: