L has try-catch-finally style exception handling.
Source:
Expand


namespace Except is
    use System.Exception;

    class Main is
        void init() is            
            try
                IO.Std.out.println( "try..." );
                fail();
                IO.Std.out.println( "not reachable" );
            catch Exception e
                // Any System.Exception or subclass of it thrown within the try
                // block any not caught will be caught here and assigned to the
                // variable 'e'
                // (Since 'throw' may only throw System.Exception and its
                // subclasses, this catch will match all exceptions)

                IO.Std.out.println( "oops: " + e );
            finally
                // Statements in a finally block are always executed regardless
                // of whether any exception is caught or passed by the above catch.

                IO.Std.out.println( "finally" );
            yrt
        si

        // this method will fail and throw an exception     
        void fail() is
            IO.Std.out.println( "will fail when i = 5..." );
            var a = new int[5];
            foreach var i; 0..49 do
                IO.Std.out.println( "will set a[" + i + "] = " + i + "..." );
                a[i] = i; // oops: array subscript out of bounds
            od
            IO.Std.out.println( "not reachable" );
        si
    si
si
Output:
Expand
try...
will fail when i = 5...
will set a[0] = 0...
will set a[1] = 1...
will set a[2] = 2...
will set a[3] = 3...
will set a[4] = 4...
will set a[5] = 5...
oops: System.ArrayBoundsException: array index out of bounds
/var/giantblob/lsp/lsp.fcgi(_ZN6System20ArrayBoundsException11throwBoundsEv+0x32) [0x49d9a2]
[0x7f539308ef19]
[0x7f539308f0a6]
/var/giantblob/lsp/lsp.fcgi(_ZN6System5Class6createEv+0x58) [0x4c2fb8]
/var/giantblob/lsp/lsp.fcgi(_ZN7LSPPage9Giantblob7exampleEN6System5ClassE+0xc2) [0x4b9b12]
[0x7f539308ec41]
/var/giantblob/lsp/lsp.fcgi(_ZN4HTML4Page5onGetEv+0x56) [0x4a8376]
/var/giantblob/lsp/lsp.fcgi(_ZN4HTML7Request10renderPageEN6System6StringEb+0x78d) [0x4a7dad]
/var/giantblob/lsp/lsp.fcgi(_ZN4HTML7Request7processEv+0xc47) [0x4a7557]
/var/giantblob/lsp/lsp.fcgi(_ZN4HTML4Main4initEv+0x138) [0x49e2a8]
/var/giantblob/lsp/lsp.fcgi(_ZN6System7Startup4run1EN6System6ObjectE+0xd) [0x4c804d]
/var/giantblob/lsp/lsp.fcgi(main+0x3e) [0x48582e]

finally
site design and content copyright (C) jeek 1995-2010     [ 13132 ]