Skip to content

Commit a73fd77

Browse files
committed
Allow try expression without catch or finally, issue warning
1 parent 70a8426 commit a73fd77

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,13 @@ object Parsers {
10101010
expr()
10111011
} else EmptyTree
10121012
val finalizer =
1013-
if (handler.isEmpty || in.token == FINALLY) { accept(FINALLY); expr() }
1014-
else EmptyTree
1013+
if (in.token == FINALLY) { accept(FINALLY); expr() }
1014+
else {
1015+
if (handler.isEmpty)
1016+
warning("A try without `catch` or `finally` is equivalent to putting its body in a block; no exceptions are handled.")
1017+
1018+
EmptyTree
1019+
}
10151020
ParsedTry(body, handler, finalizer)
10161021
}
10171022
case THROW =>

tests/pos/tryWithoutHandler.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
try {
4+
println("hello")
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)