Thread: Try-catch works with structured exception

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

    Try-catch works with structured exception

    Hello everyone,


    I have tested try-catch works with structured exception, to my surprise. Previously I think we have to use __try and __except.

    Any comments? Here is my test code and I am using Visual Studio 2008.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int* address = NULL;
    
    	try{
    
    		(*address) = 1024;
    	} catch (...)
    	{
    		cout << "access violation caught" << endl;
    	}
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and why do you think it is a structural exception?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Disable backward compatibility mode. VC++ 5 foolishly caught SEs with catch(...), and every MS compiler since was forced to retain this behaviour. In VC++ 2002 they finally made it configurable.

    MinGW will not catch SEs with a catch(...).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    I compile with /EHa and I catch the access violation exception, so I think it is a structured exception.

    Quote Originally Posted by vart View Post
    and why do you think it is a structural exception?

    regards,
    George

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


    1. You mean catch(...) is not the standard way to catch structured exception and we should use __except instead?

    2. How about try? The same as catch(...) for backward compatibility? And we should use __try instead?

    Quote Originally Posted by CornedBee View Post
    Disable backward compatibility mode. VC++ 5 foolishly caught SEs with catch(...), and every MS compiler since was forced to retain this behaviour. In VC++ 2002 they finally made it configurable.

    MinGW will not catch SEs with a catch(...).

    regards,
    George

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Given that there is no standard covering SEH, why would there be a standard way to catch SEs?

    No, the only valid way to catch SEs is with __except.

    As for try and __try, it's a matter of syntactic matching. You introduce a C++ try block with try; you introduce an SEH try block with __try.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    1. I want to confirm that you point is to use __try and __except to make the code more elegant, right?

    2. I heard when using structured exception, there is no stack unwinding? Right?

    (so RAII can not be used?)

    Quote Originally Posted by CornedBee View Post
    Given that there is no standard covering SEH, why would there be a standard way to catch SEs?

    No, the only valid way to catch SEs is with __except.

    As for try and __try, it's a matter of syntactic matching. You introduce a C++ try block with try; you introduce an SEH try block with __try.

    regards,
    George

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1. The hell? Where did you pick that up? Use C++ exceptions and leave SEH to specialized Win32 tasks. 99.99&#37; of Windows programs don't use SEH, and I bet 80% of Windows programmers haven't even heard of it.

    2. Well, consider this: C++ exception under MSVC are implemented using SEH. Do you think it supports stack unwinding?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    1.

    Sorry for my limited knowledge. I read your post again and I think if we need to use SEH in special case, you prefer to use __try/__except other than using try/catch right? If I wrongly understand your points, please feel free to correct me.

    2.

    I heard from other people, I will write some code to test and let you know the result soon. :-)

    Quote Originally Posted by CornedBee View Post
    1. The hell? Where did you pick that up? Use C++ exceptions and leave SEH to specialized Win32 tasks. 99.99% of Windows programs don't use SEH, and I bet 80% of Windows programmers haven't even heard of it.

    2. Well, consider this: C++ exception under MSVC are implemented using SEH. Do you think it supports stack unwinding?

    regards,
    George

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


    I have tested that you are correct and when there is structured exception, during stack unwinding, destructor is called for local object. It is apprciated if you could help to review whether my code is correct to verify this purpose? :-)

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Foo
    {
    public:
    	Foo()
    	{
    		cout << "constructing Foo" << endl;
    	}
    
    	virtual ~Foo()
    	{
    		cout << "destructing Foo" << endl;
    	}
    
    };
    
    int main()
    {
    	int* address = NULL;
    
    	try{
    
    		Foo foo1;
    		(*address) = 1024;
    	} catch (...)
    	{
    		cout << "access violation caught" << endl;
    	}
    	return 0;
    }
    Output:

    constructing Foo
    destructing Foo
    access violation caught

    Quote Originally Posted by CornedBee View Post
    1. The hell? Where did you pick that up? Use C++ exceptions and leave SEH to specialized Win32 tasks. 99.99% of Windows programs don't use SEH, and I bet 80% of Windows programmers haven't even heard of it.

    2. Well, consider this: C++ exception under MSVC are implemented using SEH. Do you think it supports stack unwinding?

    regards,
    George

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, but it's still wrong to catch SEs with catch(...).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    Seems we can not use structured exception to unwind local object, here is the new code and compile error message. Are we wrong before? :-)

    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;
    }
    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

    Quote Originally Posted by CornedBee View Post
    Yes, but it's still wrong to catch SEs with catch(...).

    regards,
    George

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    http://forums.microsoft.com/MSDN/Sho...40908&SiteID=1

    But then catch(...) catches SEs again.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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


    I have read through this thread and it deals majorly with if an exception is not caught by main, and I do not think it relates to my original question.

    My question is simple,

    1. try/catch can catch structured exception;

    2. __try/__catch can also catch structured exception.

    Is it correct to use them both?

    Quote Originally Posted by CornedBee View Post
    http://forums.microsoft.com/MSDN/Sho...40908&SiteID=1

    But then catch(...) catches SEs again.

    regards,
    George

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    1. try/catch can catch structured exception;
    try/catch is part of the C++ standard. There's no such thing as a structured exception.
    In Visual C++, if you compile with /Eha, catch(...) will catch structured exceptions.

    2. __try/__catch can also catch structured exception.
    __try/__except.

    Is it correct to use them both?
    I believe that SEH should not be used in C++ at all, except for some very, very specific use cases.

    The page I linked to contains more information on the requirements of using both SEH and C++ EH in the same program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception and global variable
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 01-28-2008, 07:12 PM
  2. Replies: 5
    Last Post: 06-06-2007, 11:10 PM
  3. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM
  4. try
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-20-2002, 08:46 AM
  5. Developing Custom Exception Handler :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2002, 04:21 PM