Thread: Throw statement without a try statment to accompany it.

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Exclamation Throw statement without a try statment to accompany it.

    Hi there,

    I've found some odd code in one of my projects.

    Code:
    #if defined(DEBUG) || defined(_DEBUG) 
    	// Enable the D3D12 debug layer.
    {
    	ComPtr<ID3D12Debug> debugController;
    	ThrowIfFailed(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)));
            D3D12GetDebugInterface(IID_PPV_ARGS(&debugController));
    	debugController->EnableDebugLayer();
    }
    ThrowIfFailed is defined as:

    Code:
    #ifndef ThrowIfFailed
    #define ThrowIfFailed(x)                                              \
    {                                                                     \
        HRESULT hr__ = (x);                                               \
        std::wstring wfn = AnsiToWString(__FILE__);                       \
        if(FAILED(hr__)) { throw DxException(hr__, L#x, wfn, __LINE__); } \
    }
    #endif
    And the class constructor passed to the throw call initializes the following class:

    Code:
    class DxException
    {
    public:
        DxException() = default;
        DxException(HRESULT hr, const std::wstring& functionName, const std::wstring& filename, int lineNumber);
    
        std::wstring ToString()const;
    
        HRESULT ErrorCode = S_OK;
        std::wstring FunctionName;
        std::wstring Filename;
        int LineNumber = -1;
    };
    All seems ok. Only trouble is I thought a throw call was usually in a block with a try statement. I can't find a try statement anywhere.

    How is throw allowed to call like that without a try statement?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The try/catch of last resort in the runtime that called your main will catch it, and just kill your program.

    In effect, there is something like this where your main is called.
    Code:
    try 
    {
        int r = main(argc,argv);
    } 
    catch (const std::exception& e) 
    {
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Epic answer, thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple if statment need help
    By automagp68 in forum C Programming
    Replies: 9
    Last Post: 02-07-2012, 01:45 PM
  2. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  3. ¿What is happening with this If statment?
    By Isolda_ in forum C Programming
    Replies: 4
    Last Post: 09-16-2007, 04:05 PM
  4. If statment +text
    By Zewu in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2002, 04:31 PM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM

Tags for this Thread