Why doesn't Dart SDK allow to get value of message field from Exception class? The Error class even doesn't have message field.
Currently, we must specify child Exception and Error to get the message.
try {
//...
} on AssertionError catch(e) {
e.message; //OK
}
How can I get the message for all errors and exceptions?
try {
//...
} catch(e) {
e.message; // There is no getter for message field??? Error class even doesn't have message field???
}
I know that there is e.toString(), but it usually contains other information that we don't want sometime.
e is caught as an Object, apparently, which does not have a message field. I think Effective Dart has some good advice and explanations on the subject. The Error class is a parent type to many errors, but it also doesn't have message. toString is as good as you're going to get on an unknown error type. Why do you sometimes not want the information provided there?
Neither the Error class nor the Exception class have a message field.
Not all errors and exceptions have messages to expose, because they don't need them.
Exceptions are intended to be caught and handled programmatically, not printed and ignored. They represent the message by their fields, which may contain actual data. That can be anything. There is not even a promise that toString does anything useful.
The exception class constructor is not recommended for actual code. It's available so you can wrap up a quick exception object during development, but you should not be using it in your production code. It's a useless exception because it doesn't tell you what went wrong. A real exception object would contain domain specific data about the exceptional case.
Errors are intended to be printed, but not "handled". There is nothing to handle, your program might be in a bad state afterwards, and the safest thing you can do is to crash the entire thing. The toString is the intended API. Some errors have a message field because they can carry a text to put into the toString, but for example the RangeError doesn't really need a message field. It inherits it one ArgumentError, but it will only contain a small part of the entire toString message because the remaining is also contained in the min, max and value fields.
If you are catching anything, then also remember that Dart can throw any (non-null) object. It may not be just Errors or classes implementing Exception. The only API you can trust is toString.
Thank you guys for your useful reference and nice explanation!
I totally agree with your comments.
So my next question is, how could I know if I am missing any exception?
How can the IDE, Dart Analysis or any tool somehow tell/warn me?
Dart does not include exceptions in the type signatures, so you have to read the method documentation (or possibly class or library documentation, if they say that "all foo methods throw FooException if foo happens").
It's technically possible to scan function DartDoc for paragraps of the form:
/// Throws (a) [ExceptionType] if/when ....
and warn you if a call to the function doesn't catch ExceptionType. We don't currently do that.
(That's the format that we intend to use for documenting exceptions in the SDK code, but a lot of code was written before we came up with that.)
It's technically possible to scan function DartDoc for paragraps of the form:
/// Throws (a) [ExceptionType] if/when ....and warn you if a call to the function doesn't catch
ExceptionType.
If it happens, I'm not sure if it could be a performance problem, because exceptions are usually in nested DartDoc and could be at a very deep level of the chain.
We don't currently do that.
Is that feature in the plan of Dart team?
No current plan that I'm aware of. The structured way to write DartDoc was never really embraced, so we haven't done anything with it.