Line | |
---|
1 | import sys
|
---|
2 | from typing import List
|
---|
3 |
|
---|
4 |
|
---|
5 | class TryCatch:
|
---|
6 | # Tests try/catch
|
---|
7 | def __init__(self):
|
---|
8 | self.test1()
|
---|
9 | self.test2()
|
---|
10 | self.test3()
|
---|
11 | @staticmethod
|
---|
12 | def main(args:List[str]) -> None:
|
---|
13 | '''
|
---|
14 |
|
---|
15 | @param args the arguments of the call.
|
---|
16 | '''
|
---|
17 | TryCatch()
|
---|
18 | def test1(self) -> None:
|
---|
19 | try:
|
---|
20 | x:int = 1 / 0
|
---|
21 | sys.stdout.write("div0 did not throw?!"+'\n')
|
---|
22 | return
|
---|
23 | except ArithmeticError as e:
|
---|
24 | sys.stdout.write("ok1"+'\n')
|
---|
25 | def test2(self) -> None:
|
---|
26 | try:
|
---|
27 | '''
|
---|
28 | if stmt is to trick java compiler into accepting
|
---|
29 | the code after the throw,
|
---|
30 | which is safety net for testing the python translation.
|
---|
31 | '''
|
---|
32 | if True:
|
---|
33 | raise ArithmeticError("test")
|
---|
34 | sys.stdout.write("throw phrase did not throw?!"+'\n')
|
---|
35 | return
|
---|
36 | except ArithmeticError as e:
|
---|
37 | sys.stdout.write("ok2"+'\n')
|
---|
38 | def test3(self) -> None:
|
---|
39 | try:
|
---|
40 | self.__dothrow()
|
---|
41 | sys.stdout.write("throw phrase did not throw?!"+'\n')
|
---|
42 | return
|
---|
43 | except BaseException as e:
|
---|
44 | sys.stdout.write("ok3"+'\n')
|
---|
45 | def __dothrow(self) -> None:
|
---|
46 | raise ArithmeticError("test")
|
---|
47 |
|
---|
48 | if __name__ == "__main__":
|
---|
49 | TryCatch.main(sys.argv[1:])
|
---|
Note:
See
TracBrowser
for help on using the repository browser.