why exception in not caught in below code snippet?

This is a discussion on why exception in not caught in below code snippet? within the C++ Programming forums, part of the General Programming Boards category; When exception is thrown in a function and not handled there, it is passed to calling function where it can ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    why exception in not caught in below code snippet?

    When exception is thrown in a function and not handled there, it is passed to calling function where it can be handled...exceptoin occurs in A() which shd get handled by catch block in main which doesnt happen ....Why?


    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    int i;int j;
    public: 
    A(int ii,int jj)
    {
      i=ii;
      j=jj;
      if(i<0)
        throw exception(); // It should be passed in main function 
    
    }
    
    
    };
    
    int main()
    {
      A a1(-2,1);
      try
      {
      }
      catch(...) //It should actually catch the exception thrown by constructor 
      {             // but it doesn't happen...Pls clarify
        cout<<"Exception caught in main";
      }
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are creating the object outside of the try block. It should be:
    Code:
    try
    {
        A a1(-2,1);
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    no it doesnt work

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by forumuser View Post
    no it doesnt work
    What didn't work? Did it not compile? Did the exception not get thrown?

    Post your modified code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    didnt compile

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    This looks very similiar to another thread that you currently have open.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by forumuser View Post
    didnt compile
    You're telling me that if you move the construction of the a1 variable into the try block that it won't compile? Sounds like you have a broken compiler, or you're not telling me the whole story.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    I suspect that further down in the function (in code that is not shown here) a1 is being used, and since it was declared inside the try block, it's not visible outside it.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    I could come to some conlusion after experimenting this.

    When exception is thrown in a method and not immediately caught, terminate method is called.
    But, if the exceptoin is caught and rethrown from CATCH BLOCK, it is propagated to the called method i.e. main method in this case...See belo code.....

    If you have other views, pls do reply to this thread.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class A
    {
    int i;int j;
    public: 
    A(int ii,int jj)
    {
      i=ii;
      j=jj;
      if(i<=0)
    	 throw exception();
        try
    	{}
    	catch(...)
    	{
    	 throw;
    	}
    
    };
    
    int main()
    {
      try
      {
    	  A a1(-3,1); 
      
      }
      catch(...)
      {
        cout<<"Exception caught in main";
      }
    
    
    
    }

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can safely remove

    Code:
        try
    	{}
    	catch(...)
    	{
    	 throw;
    	}
    since that does absolutely nothing. A try block means that you try to execute some code (between the braces), and catch exceptions thrown by that code. An empty block cannot throw any exceptions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by forumuser
    When exception is thrown in a method and not immediately caught, terminate method is called.
    If that is so, then you must always have a try/catch whenever you want to throw an exception, which is a rather silly requirement. Rather, this is what the C++ Standard says:
    Quote Originally Posted by C++03 Section 15.1 Paragraph 8
    If no exception is presently being handled, executing a throw-expression with no operand calls terminate().
    This is precisely what anon pointed out in post #10 as problematic in your example.

    EDIT:
    Oh wait, that is not quite right: since that throw is in a catch block, that should not be the problem. Rather, anon's analysis that the code is a no-op should be correct.
    Last edited by laserlight; 09-18-2009 at 11:36 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code snippet which I need help understanding
    By c_me in forum C Programming
    Replies: 3
    Last Post: 07-28-2006, 06:06 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21