-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Better try
and catch
messages
#1509
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
Better try
and catch
messages
#1509
Conversation
b4881bf
to
3b1a333
Compare
|
||
handler match { | ||
case Block(Nil, EmptyTree) => syntaxError( | ||
"`catch` block does not contain a valid expression, try adding a case like - `case t: Throwable =>` to the block", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People should not use Throwable
unless they know what they're doing, I would suggest case e: Exception =>
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better: case scala.util.control.NonFatal(e) =>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not against that, but do we really want to recommend people to do that by default? NonFatal
will catch StackOverflowException
among other things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't:
scala> try { throw new StackOverflowError } catch { case scala.util.control.NonFatal(e) => println("I caught you!") }
java.lang.StackOverflowError
at .liftedTree1$1(<console>:8)
... 33 elided
NonFatal
is precisely meant as a good default, to catch all the things you should catch, and none of those you shouldn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha you're right, I googled to find the doc and didn't realize I was looking at the 2.9 version :) scala-lang.org/api/2.9.3/scala/util/control/NonFatal$.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM
3b1a333
to
e8e7099
Compare
@@ -0,0 +1,3 @@ | |||
object Test { | |||
try {} catch {} // error: `catch` block does not contain a valid expression, try adding a case like - `case t: Throwable =>` to the block |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You left Throwable here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - not sure why github is displaying the old code here...
e8e7099
to
d1eeb5f
Compare
Fixes #1446, and allows a
try
without acatch
orfinally
as per the spec.