File tree Expand file tree Collapse file tree 3 files changed +35
-3
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -1936,14 +1936,20 @@ class Typer extends Namer
1936
1936
}
1937
1937
var checkedArgs = preCheckKinds(args1, paramBounds)
1938
1938
// check that arguments conform to bounds is done in phase PostTyper
1939
- if (tpt1.symbol == defn.andType)
1939
+ val tycon = tpt1.symbol
1940
+ if (tycon == defn.andType)
1940
1941
checkedArgs = checkedArgs.mapconserve(arg =>
1941
1942
checkSimpleKinded(checkNoWildcard(arg)))
1942
- else if (tpt1.symbol == defn.orType)
1943
+ else if (tycon == defn.orType)
1943
1944
checkedArgs = checkedArgs.mapconserve(arg =>
1944
1945
checkSimpleKinded(checkNoWildcard(arg)))
1946
+ else if tycon == defn.throwsAlias
1947
+ && checkedArgs.length == 2
1948
+ && checkedArgs(1 ).tpe.derivesFrom(defn.RuntimeExceptionClass )
1949
+ then
1950
+ report.error(em " throws clause cannot be defined for RuntimeException " , checkedArgs(1 ).srcPos)
1945
1951
else if (ctx.isJava)
1946
- if (tpt1.symbol eq defn.ArrayClass ) then
1952
+ if tycon eq defn.ArrayClass then
1947
1953
checkedArgs match {
1948
1954
case List (arg) =>
1949
1955
val elemtp = arg.tpe.translateJavaArrayElementType
Original file line number Diff line number Diff line change
1
+ -- Error: tests/neg/i13846.scala:3:22 ----------------------------------------------------------------------------------
2
+ 3 |def foo(): Int throws ArithmeticException = 1 / 0 // error
3
+ | ^^^^^^^^^^^^^^^^^^^
4
+ | throws clause cannot be defined for RuntimeException
5
+ -- Error: tests/neg/i13846.scala:7:9 -----------------------------------------------------------------------------------
6
+ 7 | foo() // error
7
+ | ^
8
+ | The capability to throw exception ArithmeticException is missing.
9
+ | The capability can be provided by one of the following:
10
+ | - A using clause `(using CanThrow[ArithmeticException])`
11
+ | - A `throws` clause in a result type such as `X throws ArithmeticException`
12
+ | - an enclosing `try` that catches ArithmeticException
13
+ |
14
+ | The following import might fix the problem:
15
+ |
16
+ | import unsafeExceptions.canThrowAny
17
+ |
Original file line number Diff line number Diff line change
1
+ import language .experimental .saferExceptions
2
+
3
+ def foo (): Int throws ArithmeticException = 1 / 0 // error
4
+
5
+ def test (): Unit =
6
+ try
7
+ foo() // error
8
+ catch
9
+ case _ : ArithmeticException => println(" Caught" )
You can’t perform that action at this time.
0 commit comments