Skip to content

Commit d1eeb5f

Browse files
committed
Improve error message on empty catch block
1 parent a73fd77 commit d1eeb5f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,15 @@ object Parsers {
10091009
in.nextToken()
10101010
expr()
10111011
} else EmptyTree
1012+
1013+
handler match {
1014+
case Block(Nil, EmptyTree) => syntaxError(
1015+
"`catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block",
1016+
handler.pos
1017+
)
1018+
case _ =>
1019+
}
1020+
10121021
val finalizer =
10131022
if (in.token == FINALLY) { accept(FINALLY); expr() }
10141023
else {

tests/neg/emptyCatch.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
try {} catch {} // error: `catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block
3+
}

0 commit comments

Comments
 (0)