@@ -3,6 +3,7 @@ package dotc
3
3
package reporting
4
4
5
5
import dotc .core .Contexts .Context
6
+ import dotc .printing .SyntaxHighlighting ._
6
7
7
8
object ErrorExplanations {
8
9
import dotc .ast .Trees ._
@@ -36,32 +37,40 @@ object ErrorExplanations {
36
37
case Block (Nil , untpd.EmptyTree ) => " {}"
37
38
case _ => tryBody.show
38
39
}
39
- s """ |Explanation:
40
- |============
41
- |A try expression should be followed by some mechanism to handle any exceptions
42
- |thrown. Typically a `catch` expression follows the `try` and pattern matches
43
- |on any expected exceptions. For example:
44
- |
45
- |try $tryString catch {
46
- | case t: Throwable => ???
47
- |}
48
- |
49
- |It is also possible to follow a `try` immediately by a finally - letting the
50
- |exception propagate - but still allowing for some clean up in `finally`:
51
- |
52
- |try $tryString finally {
53
- | // perform your cleanup here!
54
- |} """ .stripMargin
40
+
41
+ val code1 =
42
+ s """ |try $tryString catch {
43
+ | case t: Throwable => ???
44
+ |} """ .stripMargin
45
+
46
+ val code2 =
47
+ s """ |try $tryString finally {
48
+ | // perform your cleanup here!
49
+ |} """ .stripMargin
50
+
51
+ hl """ |Explanation:
52
+ |============
53
+ |A ${" try" } expression should be followed by some mechanism to handle any exceptions
54
+ |thrown. Typically a ${" catch" } expression follows the ${" try" } and pattern matches
55
+ |on any expected exceptions. For example:
56
+ |
57
+ | $code1
58
+ |
59
+ |It is also possible to follow a ${" try" } immediately by a ${" finally" } - letting the
60
+ |exception propagate - but still allowing for some clean up in ${" finally" }:
61
+ |
62
+ | $code2
63
+ """ .stripMargin
55
64
}
56
65
}
57
66
58
67
case class EmptyCatchBlock (tryBody : untpd.Tree )(implicit ctx : Context ) extends EmptyCatchOrFinallyBlock (tryBody) {
59
68
val msg =
60
- " ` catch` block does not contain a valid expression, try adding a case like - `case e: Exception =>` to the block"
69
+ hl """ The ${ " catch" } block does not contain a valid expression, try adding a case like - ` ${ " case e: Exception =>" } ` to the block "" "
61
70
}
62
71
63
72
case class EmptyCatchAndFinallyBlock (tryBody : untpd.Tree )(implicit ctx : Context ) extends EmptyCatchOrFinallyBlock (tryBody) {
64
73
val msg =
65
- " A try without ` catch` or ` finally` is equivalent to putting its body in a block; no exceptions are handled."
74
+ hl """ A ${ " try" } without ${ " catch" } or ${ " finally" } is equivalent to putting its body in a block; no exceptions are handled. "" "
66
75
}
67
76
}
0 commit comments