[Java] Exception: try-catch-finally

1219阅读 0评论2011-11-16 web_surf
分类:Java

  1. try-block
    In general, statements that may cause exception will be placed in try-block
  2. catch-block
    You can catch some of exception that may be threw in try-block. If an exception is catched, the work flow is "try -> catch -> finally (if exists) -> statements after try-catch-finally -> return; othewise, the work flow is "try -> finally (if exits) -> return immediately;
  3. finally-block
    You need to do some clean up task here. No matter if any exception is threw in try-block or not, no matter if the exception is catched in catch-block, statements in finally-block will be executed. i.e:
    SomeType a = null;
    try
    {
        a = new SomeType();
        a.xxx();
        ...
    }
    catch (XXX e)
    {
    }
    finally
    {
        if (a != null) {destroy a;}
    }
  4. Statements after try-catch-block:
    if an exception is threw and it is not catched, the statements after try-catch-finally block won't be executed, the method will be return immediatelly once the finally-clock is executed if it exists.
  5. If an exception is not catched, you must declare this behind method declaration statement, such as xxx 'public void myMethod(xxx) throws AException, BException, ...'
上一篇:[Linux] Share Files
下一篇:[Android] Widgets: RemoteViews