-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document Scala compiler error codes #24734
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
base: main
Are you sure you want to change the base?
Changes from all commits
1b3d6bc
cc89c51
1b7a3e6
3521254
7dc49e4
61b838c
683dbff
799aa84
a85ced0
0040b6e
e3f3f86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| --- | ||
| title: E001: Empty Catch Block | ||
| kind: Error | ||
| --- | ||
| # E001: Empty Catch Block | ||
|
|
||
| This error is emitted when a `try` expression has a `catch` block that does not contain any case handlers. | ||
|
|
||
| A `try` expression should be followed by some mechanism to handle any exceptions | ||
| thrown. Typically a `catch` expression follows the `try` and pattern matches | ||
| on any expected exceptions. For example: | ||
|
|
||
| ```scala | ||
| try { | ||
| println("hello") | ||
| } catch { | ||
| case e: Exception => ??? | ||
| } | ||
| ``` | ||
|
|
||
| It is also possible to follow a `try` immediately by a `finally` - letting the | ||
| exception propagate - but still allowing for some clean up in `finally`: | ||
|
|
||
| ```scala | ||
| try { | ||
| println("hello") | ||
| } finally { | ||
| // perform your cleanup here! | ||
| } | ||
| ``` | ||
|
|
||
| It is recommended to use the `NonFatal` extractor to catch all exceptions as it | ||
| correctly handles transfer functions like `return`. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might need an example? What is transfer function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea, example and description was generated based on |
||
|
|
||
| --- | ||
|
|
||
| ## Example | ||
|
|
||
| ```scala sc:fail sc-opts:-explain | ||
| def example() = | ||
| try { | ||
| println("hello") | ||
| } catch { } | ||
| ``` | ||
|
|
||
| ### Error | ||
|
|
||
| ```scala sc:nocompile | ||
| -- [E001] Syntax Error: example.scala:4:4 -------------------------------------- | ||
| 4 | } catch { } | ||
| | ^^^^^^^^^ | ||
| | The catch block does not contain a valid expression, try | ||
| | adding a case like - case e: Exception => to the block | ||
| |----------------------------------------------------------------------------- | ||
| | Explanation (enabled by `-explain`) | ||
| |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| | A try expression should be followed by some mechanism to handle any exceptions | ||
| | thrown. Typically a catch expression follows the try and pattern matches | ||
| | on any expected exceptions. For example: | ||
| | | ||
| | import scala.util.control.NonFatal | ||
| | | ||
| | try { | ||
| | println("hello") | ||
| | } catch { | ||
| | case NonFatal(e) => ??? | ||
| | } | ||
| | | ||
| | It is also possible to follow a try immediately by a finally - letting the | ||
| | exception propagate - but still allowing for some clean up in finally: | ||
| | | ||
| | try { | ||
| | println("hello") | ||
| | } finally { | ||
| | // perform your cleanup here! | ||
| | } | ||
| | | ||
| | It is recommended to use the NonFatal extractor to catch all exceptions as it | ||
| | correctly handles transfer functions like return. | ||
| ----------------------------------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| ```scala sc:compile | ||
| // Remove redundant 'try' block | ||
| def example() = | ||
| println("hello") | ||
| ``` | ||
|
|
||
| ```scala sc:compile | ||
| // Alternative: Add a case handler to catch exceptions | ||
| import scala.util.control.NonFatal | ||
|
|
||
| def example() = | ||
| try println("hello") | ||
| catch { case NonFatal(e) => println(s"Caught: $e") } | ||
| ``` | ||
|
|
||
| ```scala sc:compile | ||
| // Alternative: use finally instead if you only need cleanup | ||
| def example() = | ||
| try println("hello") | ||
| finally println("cleanup") | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| --- | ||
| title: E002: Empty Catch And Finally Block | ||
| kind: Warning | ||
| --- | ||
| # E002: Empty Catch And Finally Block | ||
|
|
||
| This warning is emitted when a `try` expression has neither a `catch` block nor a `finally` block. Such a `try` is redundant since no exceptions are handled. | ||
|
|
||
| A `try` expression should be followed by some mechanism to handle any exceptions | ||
| thrown. Typically a `catch` expression follows the `try` and pattern matches | ||
| on any expected exceptions. For example: | ||
|
|
||
| ```scala | ||
| try { | ||
| println("hello") | ||
| } catch { | ||
| case e: Exception => ??? | ||
| } | ||
| ``` | ||
|
|
||
| It is also possible to follow a `try` immediately by a `finally` - letting the | ||
| exception propagate - but still allowing for some clean up in `finally`: | ||
|
|
||
| ```scala | ||
| try { | ||
| println("hello") | ||
| } finally { | ||
| // perform your cleanup here! | ||
| } | ||
| ``` | ||
|
|
||
| It is recommended to use the `NonFatal` extractor to catch all exceptions as it | ||
| correctly handles transfer functions like `return`. | ||
|
|
||
| --- | ||
|
|
||
| ## Example | ||
|
|
||
| ```scala sc:fail sc-opts:-explain,-Werror | ||
| @main def example() = | ||
| try { | ||
| println("hello") | ||
| } | ||
| ``` | ||
|
|
||
| ### Warning | ||
|
|
||
| ```scala sc:nocompile | ||
| -- [E002] Syntax Warning: example.scala:2:2 ------------------------------------ | ||
| 2 | try { | ||
| | ^ | ||
| | A try without catch or finally is equivalent to putting | ||
| | its body in a block; no exceptions are handled. | ||
| 3 | println("hello") | ||
| 4 | } | ||
| |----------------------------------------------------------------------------- | ||
| | Explanation (enabled by `-explain`) | ||
| |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| | A try expression should be followed by some mechanism to handle any exceptions | ||
| | thrown. Typically a catch expression follows the try and pattern matches | ||
| | on any expected exceptions. For example: | ||
| | | ||
| | import scala.util.control.NonFatal | ||
| | | ||
| | try { | ||
| | println("hello") | ||
| | } catch { | ||
| | case NonFatal(e) => ??? | ||
| | } | ||
| | | ||
| | It is also possible to follow a try immediately by a finally - letting the | ||
| | exception propagate - but still allowing for some clean up in finally: | ||
| | | ||
| | try { | ||
| | println("hello") | ||
| | } finally { | ||
| | // perform your cleanup here! | ||
| | } | ||
| | | ||
| | It is recommended to use the NonFatal extractor to catch all exceptions as it | ||
| | correctly handles transfer functions like return. | ||
| ----------------------------------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Remove redundant 'try' block | ||
| def example() = | ||
| println("hello") | ||
| ``` | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Alternative: Add a catch block to handle exceptions | ||
| import scala.util.control.NonFatal | ||
|
|
||
| def example() = | ||
| try | ||
| println("hello") | ||
| catch | ||
| case NonFatal(e) => println(s"Caught: $e") | ||
| ``` | ||
|
|
||
| ```scala sc:compile sc-opts:-Werror | ||
| // Alternative: Add a finally block for cleanup | ||
| def example() = | ||
| try | ||
| println("hello") | ||
| finally | ||
| println("cleanup") | ||
| ``` | ||
|
|
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.
Should we recoment braceless syntax in these snippets?
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.
Most of example for other error codes use bracless syntax, I can adjust this one.