aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dotty/tools/dotc/parsing/Parsers.scala18
-rw-r--r--tests/neg/emptyCatch.scala3
-rw-r--r--tests/pos/tryWithoutHandler.scala7
3 files changed, 26 insertions, 2 deletions
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala
index 378aa6ed7..a06930058 100644
--- a/src/dotty/tools/dotc/parsing/Parsers.scala
+++ b/src/dotty/tools/dotc/parsing/Parsers.scala
@@ -1009,9 +1009,23 @@ object Parsers {
in.nextToken()
expr()
} else EmptyTree
+
+ handler match {
+ case Block(Nil, EmptyTree) => syntaxError(
+ "`catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block",
+ handler.pos
+ )
+ case _ =>
+ }
+
val finalizer =
- if (handler.isEmpty || in.token == FINALLY) { accept(FINALLY); expr() }
- else EmptyTree
+ if (in.token == FINALLY) { accept(FINALLY); expr() }
+ else {
+ if (handler.isEmpty)
+ warning("A try without `catch` or `finally` is equivalent to putting its body in a block; no exceptions are handled.")
+
+ EmptyTree
+ }
ParsedTry(body, handler, finalizer)
}
case THROW =>
diff --git a/tests/neg/emptyCatch.scala b/tests/neg/emptyCatch.scala
new file mode 100644
index 000000000..60951d27a
--- /dev/null
+++ b/tests/neg/emptyCatch.scala
@@ -0,0 +1,3 @@
+object Test {
+ try {} catch {} // error: `catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block
+}
diff --git a/tests/pos/tryWithoutHandler.scala b/tests/pos/tryWithoutHandler.scala
new file mode 100644
index 000000000..ffe334984
--- /dev/null
+++ b/tests/pos/tryWithoutHandler.scala
@@ -0,0 +1,7 @@
+object Test {
+ def main(args: Array[String]): Unit = {
+ try {
+ println("hello")
+ }
+ }
+}