Results 1 to 6 of 6
  1. #1
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default Why syntax limitation with try/catch?

    With reference to if blocks, I notice that you can do this:

    Java Code:
    if (condition1) method1();
    else if (condition2) method2();
    else if (condition3) method3();
    //etc ...
    ... or this:

    Java Code:
    if (condition1) { method1(); }
    else if (condition2) { method2(); }
    else if (condition3) { method3(); }
    //etc ...
    ... and the compiler is happy either way.

    But if you try to do this (for example):

    Java Code:
    try method();
    catch (Exception1 e1) err_method1(e1);
    catch (Exception2 e2) err_method2(e2);
    catch (Exception3 e3) err_method3(e3);
    the compiler complains and insists that you do this:

    Java Code:
    try { method(); }
    catch (Exception1 e1) { err_method1(e1); }
    catch (Exception2 e2) { err_method2(e2); }
    catch (Exception3 e3) { err_method3(e3); }
    Why does it do that with try/catch but not if?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Imagine a nested try ... catch( ... ) statement:

    Java Code:
    try {
       try {
          ...
       }
       catch ( ... ) ...
    }
    catch ( ... ) ...
    Now imagine you don't need the curly brackets (i.e. a Statement instead of a Block syntactically):

    Java Code:
    try 
       try 
          ...
       catch( ... ) ...
    catch( ... ) ...
    You can't distinguish to wich try block the latest catch clause belongs anymore ...

    kind regards,

    Jos

  3. #3
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    I see, yes it's different to if, I hadn't thought of that! ... but you could distinguish which block if there was an endtry keyword!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by XmisterIS View Post
    I see ... but you could distinguish which block if there was an endtry keyword!
    Yep, but there isn't an endtry keyword; that's not the way they have defined the syntax of the language.

    kind regards,

    Jos

  5. #5
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    I know ... but they should! How dare they ... :p

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by simpson.john54 View Post
    hai.... whn thre is possibility of arsing an exception tht should be kept in try catch block syntax limitation is tht for every try thre must be one catch block, a try can hve n no of catch blocks.... even , whn u don't mention the catch block and if the exception is arised thn it generally first checks for catch block if it is not handled tht exception goes to JVM thn JVM handles tht expection tht wasthe reason we will get the abnormal termination....

    Example....


    try{



    }

    catch()
    {
    }
    catch(){
    }
    Counter example:

    Java Code:
    try
       try 
          ...
       catch ( ... ) ...
       catch ( ... ) ...
    catch( ... ) ...
    The catch block in the middle is 'dangling' if Statements would've been allowed instead of Blocks in try - catch statements.

    kind regards,

    Jos

Similar Threads

  1. try catch help
    By vividcooper in forum New To Java
    Replies: 8
    Last Post: 02-11-2010, 09:00 AM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  3. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  4. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •