Skip to content

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

Merged
merged 2 commits into from
Sep 15, 2016

Conversation

felixmulder
Copy link
Contributor

@felixmulder felixmulder commented Sep 14, 2016

Fixes #1446, and allows a try without a catch or finally as per the spec.


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",
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Copy link
Member

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) =>

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

@smarter smarter Sep 15, 2016

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

Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

@felixmulder felixmulder force-pushed the topic/better-try-catch-messages branch from 3b1a333 to e8e7099 Compare September 15, 2016 14:13
@@ -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
Copy link
Member

@smarter smarter Sep 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You left Throwable here

Copy link
Contributor Author

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...

@felixmulder felixmulder force-pushed the topic/better-try-catch-messages branch from e8e7099 to d1eeb5f Compare September 15, 2016 14:58
@felixmulder felixmulder merged commit 5d8f132 into scala:master Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants