Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Try/Catch/Finally

Part of HandlingErrorsCategory

Description

catch that error (don't let it catch you!)

Example

import std.asserterror;

int main()
{    
    try
    {
        whatever();
    }
    catch(AssertError)
    {  
        printf("Whoa!  Hold on there.  An assertion failed.\n\n");      
    }
    finally

    {        
        /* 
            The finally block executes to allow clean-up of items 
            allocated in the try block.
        */
        
        printf("This would happen after the errors are dealt with (if there are any errors).\n\n");
    }
    printf("If something happened, it wasn't enought of a problem to end the program.\n\n");   
    return 0;
}


void whatever()
{  
    assert(0); /* comment out this line to see what happens if no error occurs */
}

More Information

Try-Finally is also available in Java and C#.