File: Catch.j

package info (click to toggle)
jasmin-sable 1.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,464 kB
  • ctags: 1,903
  • sloc: java: 12,496; makefile: 126; csh: 93; sh: 93
file content (79 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download | duplicates (7)
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
; --- Copyright Jonathan Meyer 1996. All rights reserved. -----------------
; File:      jasmin/examples/Catch.j
; Author:    Jonathan Meyer, 10 July 1996
; Purpose:   Catching and throwing exceptions
; -------------------------------------------------------------------------

;
; This hows how to throw and catch Exceptions in Jasmin
;

.class public examples/Catch
.super java/lang/Object

; standard initializer
.method public <init>()V
    aload_0
    invokenonvirtual java/lang/Object.<init>()V
    return
.end method

.method public static main([Ljava/lang/String;)V

    .limit locals 3
    .limit stack 5

    ; set up a handler to catch subclasses of java.lang.Exception
    .catch java/lang/Exception from Label1 to Label2 using Handler

    ; store System.out in local variable 1
    getstatic java/lang/System/out Ljava/io/PrintStream;
    astore_1

    ; print out a message
    aload_1
    ldc " -- Before exception"
    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

    ; construct an instance of Exception, initialize it with a string,
    ; throw it. This is like the Java statement :
    ;
    ;     throw new Exception("My exception");
    ;

Label1:
    new java/lang/Exception
    dup
    ldc "<my exception>"
    invokenonvirtual java/lang/Exception/<init>(Ljava/lang/String;)V
    athrow

Label2:
    aload_1
    ldc " -- After exception"
    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

    return

    ; This is the handler for the exception

Handler:
    ; store the exception in local variable 2
    astore_2

    ; print out a message
    aload_1
    ldc " -- Caught exception: "
    invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V

    ; call getMessage() to retrieve the message from the Exception...
    aload_1
    aload_2
    invokevirtual java/lang/Throwable/getMessage()Ljava/lang/String;
    ; ... now print it
    invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

    ; return to the code
    goto Label2

.end method