summaryrefslogtreecommitdiff
path: root/test/files/run/try.scala
blob: a4fdfd796b954d7799adba93ada9f9150a18c22b (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
object Test extends AnyRef with App {
  val x = 1;

  def try1 = {
    Console.print("1 + 1 = ");
    Console.println(1 + (
      try {
        x;
      } catch {
        case _: Error => 1;
      }
    ));
  }

  def try2 = {
    Console.print("1 + 1 = ");
    Console.println(
      (try { x } catch {
        case _: Error => 1;
      })
      +
      (try { x } catch {
        case _: Error => 1;
      })
    );
  }

  var n = 0;

  def try3 = {
    Console.print("1 + 1 = ");
    val x = try { 1 } catch {
      case e: Error => 1;
    }
    this.n = try { 1 } catch {
      case e: Error => 1;
    }
    Console.println(x + n);
  }

  var instance: AnyRef = null;

  def try4 = {
    if (instance == null) {
      instance = try {
        "" //new String();
      } catch {
        case _: Throwable =>
          val cs = "aaa";
          if (cs.length() > 0) {
            "" //new String();
          } else {
            throw new Error("fatal error");
            null
          }
      }
    }
  }

  def try5 = try {
    Console.print("1 + 1 = ");
    try {
      if (true)
        sys.error("exit");
      1+1;
      ()
    } catch {
      case _: Throwable =>
        Console.println("2");
        sys.error("for good");
    }
    Console.println("a");
  } catch {
    case _: Throwable => ();
  }

  class A {
    private val result = {
      val y = try { x } catch {
          case _: Error => 1;
        };
      x + y
    }
    Console.print("1 + 1 = ");
    Console.println(result);
  }

  // ticket #981
  def try6 {
   class SekwencjaArray {
    def get = null
   }

   var sekw : SekwencjaArray =
     try {
       null
     } catch {
       case _: Throwable => null
     }

    new AnyRef {
      def getValueAt(row:Int, col:Int) = sekw.get
    }
  }

/*
  def finally1 = {
    Console.print("1 + 1 = ");
    Console.println(1 + (
      try {
        x
      } finally {
        ()
      }
    ));
  }

*/

  try1;
  try2;
  try3;
  try4;
  try5;
  try6;
  Console.println;
  new A();
  ()
}