File: virtpatmat_try.scala

package info (click to toggle)
scala 2.11.12-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 62,924 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,135; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (47 lines) | stat: -rw-r--r-- 1,169 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
object Test extends App {
  case class A(val x: String) extends Throwable
  class B extends Exception { override def toString = "B" }
  def bla = 0

  try {
    throw new A("meh")
  } catch { // this should emit a "catch-switch"
    case y: A => println(y.x)
    case (_ : A | _ : B)  => println("B")
    case _: Throwable => println("other")
  }

  try {
    throw new B()
  } catch { // case classes and alternative flattening aren't supported yet, but could be in principle
    // case A(x) => println(x)
    case y: A => println(y.x)
    case x@((_ : A) | (_ : B))  => println(x)
    case _: Throwable => println("other")
  }

 def simpleTry {
    try {
      bla
    } catch {
      case x: Exception if x.getMessage == "test" => println("first case " + x)
      case x: Exception => println("second case " + x)
    }
  }

  def typedWildcardTry {
    try { bla } catch { case _: ClassCastException => bla }
  }

  def wildcardTry {
    try { bla } catch { case _: Throwable => bla }
  }

  def tryPlusFinally {
    try { bla } finally { println("finally") }
  }

  def catchAndPassToLambda {
    try { bla } catch { case ex: Exception => val f = () => ex }
  }
}