Question about catch

Discussion in 'C++' started by cwc5w@hotmail.com, Dec 20, 2006.

  1. Guest

    int main()
    {
    try {
    b();
    } catch(...) {
    return 1;
    } catch(int i) {
    return 2;
    }

    what ll happen to this code?
    does it always generate syntax error because of the catch(...) phase?
    or is it complier specific?

    Thanks!
    , Dec 20, 2006
    #1
    1. Advertising

  2. Andre Kostur Guest

    wrote in news:1166606607.464543.229260
    @t46g2000cwa.googlegroups.com:

    > int main()
    > {
    > try {
    > b();
    > } catch(...) {
    > return 1;
    > } catch(int i) {
    > return 2;
    > }
    >
    > what ll happen to this code?
    > does it always generate syntax error because of the catch(...) phase?
    > or is it complier specific?


    The Standard (s15.3.6) says that the catch(...) "shall be the last handler
    for its try block". (Oh, and I'm assuming that you have one more '}' to
    close the main() block)
    Andre Kostur, Dec 20, 2006
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > int main()
    > {
    > try {
    > b();
    > } catch(...) {
    > return 1;
    > } catch(int i) {
    > return 2;
    > }
    >

    missing }

    > does it always generate syntax error because of the catch(...) phase?
    > or is it complier specific?
    >

    catch(...) must be last, it doesn't make sense any other way.


    --
    Ian Collins.
    Ian Collins, Dec 20, 2006
    #3
  4. Guest

    thanks for the quick reply!
    yes, typo on the "}"
    , Dec 20, 2006
    #4
  5. Anant Guest

    I think, it will not be compiler dependent.

    Because at run time, we check all the catch blocks sequentially, and as
    soon as we find datatype of exception and catch block are same , we
    just ignore rest others.

    And once catch(...) is found it means you will never check for other
    catch blocks. So all compiler will be forcing to write catch(...) in
    the last only.


    wrote:
    > int main()
    > {
    > try {
    > b();
    > } catch(...) {
    > return 1;
    > } catch(int i) {
    > return 2;
    > }
    >
    > what ll happen to this code?
    > does it always generate syntax error because of the catch(...) phase?
    > or is it complier specific?
    >
    > Thanks!
    Anant, Dec 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Kevin Spencer
    Aug 7, 2003
  2. Sveta
    Replies:
    1
    Views:
    1,312
    Darryl L. Pierce,,,
    Feb 29, 2004
  3. John Black
    Replies:
    8
    Views:
    4,142
    Xenos
    Aug 20, 2004
  4. Adam
    Replies:
    9
    Views:
    555
    red floyd
    Feb 2, 2006
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    551
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page