compile error about destructor

This is a discussion on compile error about destructor within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, When change from __try to try, and __except(GetExceptionCode()) to catch(...), compile can pass. Compile error and code are, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    compile error about destructor

    Hello everyone,


    When change from __try to try, and __except(GetExceptionCode()) to catch(...), compile can pass.

    Compile error and code are,

    1>Compiling...
    1>main.cpp
    1>d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(30) : warning C4509: nonstandard extension used: 'main' uses SEH and 'foo1' has destructor
    1> d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(28) : see declaration of 'foo1'
    1>d:\visual studio 2008\projects\test_exception1\test_exception1\main .cpp(35) : error C2712: Cannot use __try in functions that require object unwinding

    Code:
    #include <iostream>
    #include <excpt.h>
    #include <windows.h>
    
    using namespace std;
    
    class Foo
    {
    public:
    	Foo()
    	{
    		cout << "constructing Foo" << endl;
    	}
    
    	virtual ~Foo()
    	{
    		cout << "destrucing Foo" << endl;
    	}
    
    };
    
    int main()
    {
    	int* address = NULL;
    
    	__try{
    
    		Foo foo1;
    		(*address) = 1024;
    	} __except (GetExceptionCode())
    	{
    		cout << "access violation caught" << endl;
    	}
    	return 0;
    }

    regards,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you can't use structured exception handling with objects that have a destructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    But why using try/catch is ok? It is still structured exception catched.

    Try to replace the __try to try and replace __except to catch(...).

    Quote Originally Posted by matsp View Post
    So you can't use structured exception handling with objects that have a destructor.

    --
    Mats

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    try/catch are able to CATCH a SE, but that doesn't mean that they are the same thing, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    What is the difference betwen try/catch and __try/__except when dealing with structured exception?

    Why __try/__except is more strict?

    Quote Originally Posted by matsp View Post
    try/catch are able to CATCH a SE, but that doesn't mean that they are the same thing, right?

    --
    Mats

    regards,
    George

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    perhaps this:
    Quote Originally Posted by MSDN
    Structured exception handling works with Win32 for both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, and throw statements).
    From:
    http://msdn2.microsoft.com/en-us/lib...19(VS.80).aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    It is just a walkaround to replace structured exception with C++ exception, or even using set_se_translator to convert structured exception to C++ exception.

    But it does not explain the reason why using try/catch is ok, but using __try/__except is not ok and their difference.

    Quote Originally Posted by matsp View Post

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It does: SE exceptions are (as MSDN says) not designed for C++, so it doesn't "understand" how to destroy C++ objects - only how to catch exceptions in itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    But when we replace __try with try and replace __except with catch, we still catch structured exception, and destructor is called.

    Output is,

    constructing Foo
    destructing Foo
    access violation caught

    You can see if we change the keyword, structured exception is caught and destructor is called before handler.

    Quote Originally Posted by matsp View Post
    It does: SE exceptions are (as MSDN says) not designed for C++, so it doesn't "understand" how to destroy C++ objects - only how to catch exceptions in itself.

    --
    Mats

    regards,
    George

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, isn't this the same discussion about "are structured exceptions caught by catch" that we had a few days ago?

    Yes, structured exceptions are caught by try/catch along with C++ throws [1], whilst __try/__except only catches structured exceptions.


    [1] Unless you specifically ask the compiler to NOT catch structured exceptions with C++ catch-blocks - don't know what the exact way to tell the compiler this, but recent versions of MS VC++ is able to "not catch structured exceptions with C++ catch".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Mats,


    If you remove the destructor in the class, everything is fine with __try/__except. So, I think it is dealing with destructor. It is not the same as the question I asked before.

    Any ideas?

    Quote Originally Posted by matsp View Post
    Yes, isn't this the same discussion about "are structured exceptions caught by catch" that we had a few days ago?

    Yes, structured exceptions are caught by try/catch along with C++ throws [1], whilst __try/__except only catches structured exceptions.


    [1] Unless you specifically ask the compiler to NOT catch structured exceptions with C++ catch-blocks - don't know what the exact way to tell the compiler this, but recent versions of MS VC++ is able to "not catch structured exceptions with C++ catch".

    --
    Mats

    regards,
    George

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It may not be THE SAME question, but it's related in the sense that you are asking about try/catch use for structured exceptions.

    Of course it works when you don't have a destructor, since it's not requiring any C++ specific features to resolve the exception any longer - there is no destructor to call, so there's no reason to know how to unwind the stack, and thus the structured exception can just exit the block with no ill effects.

    I guess if we summarize: If you WANT to catch structured exceptions ONLY, you can not use C++ language features within the __try/__except block. If you want to use C++ exceptions, then you can use try/catch, and if you ALSO want to catch structured exceptions, that will work as long as you have that feature enabled in the build.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    From your summarize, I think the logics is __try/__catch has no use and we should always use try/catch. :-)

    Is that your point?

    Quote Originally Posted by matsp View Post
    It may not be THE SAME question, but it's related in the sense that you are asking about try/catch use for structured exceptions.

    Of course it works when you don't have a destructor, since it's not requiring any C++ specific features to resolve the exception any longer - there is no destructor to call, so there's no reason to know how to unwind the stack, and thus the structured exception can just exit the block with no ill effects.

    I guess if we summarize: If you WANT to catch structured exceptions ONLY, you can not use C++ language features within the __try/__except block. If you want to use C++ exceptions, then you can use try/catch, and if you ALSO want to catch structured exceptions, that will work as long as you have that feature enabled in the build.

    --
    Mats

    regards,
    George

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks Mats,


    From your summarize, I think the logics is __try/__catch has no use and we should always use try/catch. :-)

    Is that your point?
    Nearly, but not exactly: __try/__except is useful in C - which doesn't support try/catch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    I have understood your points.

    There are some mysterious things that we could not explain now, but use try/catch is preferred. :-)

    Quote Originally Posted by matsp View Post
    Nearly, but not exactly: __try/__except is useful in C - which doesn't support try/catch.

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM

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