aboutsummaryrefslogtreecommitdiff
path: root/tests/run/tryPatternMatch.scala
blob: 06b469d4d33dc50c851c126c7bf1ea0afc9ed620 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.IOException
import java.util.concurrent.TimeoutException

object IAE {
  def unapply(e: Exception): Option[String] =
    if (e.isInstanceOf[IllegalArgumentException] && e.getMessage != null) Some(e.getMessage)
    else None
}

object EX extends Exception {
  val msg = "a"
  class InnerException extends Exception(msg)
}

trait ExceptionTrait extends Exception

trait TestTrait {
  type ExceptionType <: Exception

  def traitTest(): Unit = {
    try {
      throw new IOException
    } catch {
      case _: ExceptionType => println("success 9.2")
      case _                => println("failed 9.2")
    }
  }
}

object Test extends TestTrait {
  type ExceptionType = IOException

  def main(args: Array[String]): Unit = {
    var a: Int = 1

    try {
      throw new Exception("abc")
    } catch {
      case _: Exception => println("success 1")
      case _            => println("failed 1")
    }

    try {
      throw new Exception("abc")
    } catch {
      case e: Exception => println("success 2")
      case _            => println("failed 2")
    }

    try {
      throw new Exception("abc")
    } catch {
      case e: Exception if e.getMessage == "abc" => println("success 3")
      case _                                     => println("failed 3")
    }

    try {
      throw new Exception("abc")
    } catch {
      case e: Exception if e.getMessage == "" => println("failed 4")
      case _                                  => println("success 4")
    }

    try {
      throw EX
    } catch {
      case EX => println("success 5")
      case _  => println("failed 5")
    }

    try {
      throw new EX.InnerException
    } catch {
      case _: EX.InnerException => println("success 6")
      case _                    => println("failed 6")
    }

    try {
      throw new NullPointerException
    } catch {
      case _: NullPointerException | _:IOException => println("success 7")
      case _                                       => println("failed 7")
    }

    try {
      throw new ExceptionTrait {}
    } catch {
      case _: ExceptionTrait => println("success 8")
      case _                 => println("failed 8")
    }

    try {
      throw new IOException
    } catch {
      case _: ExceptionType => println("success 9.1")
      case _                => println("failed 9.1")
    }

    traitTest() // test 9.2

    def testThrow(throwIt: => Unit): Unit = {
      try {
        throwIt
      } catch {
        // These cases will be compiled as catch cases
        case e: NullPointerException                 => println("NullPointerException")
        case e: IndexOutOfBoundsException            => println("IndexOutOfBoundsException")
        case _: NoSuchElementException               => println("NoSuchElementException")
        case _: EX.InnerException                    => println("InnerException")
        // All the following will be compiled as a match
        case IAE(msg)                                => println("IllegalArgumentException: " + msg)
        case _: ExceptionTrait                       => println("ExceptionTrait")
        case e: IOException if e.getMessage == null  => println("IOException")
        case _: NullPointerException | _:IOException => println("NullPointerException | IOException")
        case `a`                                     => println("`a`")
        case EX                                      => println("EX")
        case e: IllegalArgumentException             => println("IllegalArgumentException")
        case _: ClassCastException                   => println("ClassCastException")
      }
    }

    testThrow(throw new IllegalArgumentException("abc"))
    testThrow(throw new IllegalArgumentException())
    testThrow(throw new IOException("abc"))
    testThrow(throw new NoSuchElementException())
    testThrow(throw EX)
    testThrow(throw new EX.InnerException)
    testThrow(throw new NullPointerException())
    testThrow(throw new ExceptionTrait {})
    testThrow(throw a.asInstanceOf[Throwable])
    try {
      testThrow(throw new TimeoutException)
      println("TimeoutException did not escape")
    } catch {
      case _: TimeoutException => println("TimeoutException escaped")
    }
  }

}