Thread: Exceptions and constructors

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    Question Exceptions and constructors

    I would like to convert this code to use exceptions (let's assume that in practice, this code would be more complex.). The question is, how/where would I do it? Do I catch the exception in the constructor, main(), or in one of the other functions? Where do I define the exception class? Would it be part of CTest, or another file altogether? Basically, I'd like to catch exceptions and exit execution cleanly - allowing any cleanup to be done. Any words of wisdom?

    Class definition:
    Code:
    //File: Test.h
    #include <string>
    #include <iostream>
    using namespace std;
    
    class CTest  
    {
    public:
    	string m_Welcome;
    	CTest(string str_welcome);
    	void SetWelcomeString(string welcome_str);
    	int DoSomething(void);
    };
    Class implementation:
    Code:
    //File: Test.cpp
    #include "Test.h"
    
    CTest::CTest(string welcome_str)
    {
        CTest::SetWelcomeString(welcome_str);
    	if(CTest::DoSomething())
        {
            cerr << "Error!" << endl;
            exit(1);
        }
    }
    
    void CTest::SetWelcomeString(string welcome_str)
    {
        m_Welcome = welcome_str;
    }
    
    int CTest::DoSomething(void)
    {
        //returns 0 on success, 1 on failure
        if(m_Welcome.length())
        { 
            cout << m_Welcome << endl;
            return(0);
        }
        //else
        return(1);
    }
    Main:
    Code:
    //File: ExceptTest.cpp
    #include "Test.h"
    
    int main()
    {
    	CTest testing("Hello, World!");
    	return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Catch the exception at the point in the code where something can be DONE about the failure. Catching an exception is pointless if you can do nothing to resolve it. Therefore, where and how you catch exceptions depends on the type of exception you are trying to catch, who is throwing it, and what caused it.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    As to where the exception is declared, it depends on what it represents. My policy is that if an exception only makes sense in the context of a specific class, I make it a member of the class. If the exception isn't specific to any class, I make it global.

    For example, I have a socket wrapper class that throws a number of network related exceptions. Since these exceptions will only be thrown by the socket wrapper, I made them members. Users of my socket wrapper must catch things like NetworkWrapper::CantBindSocket and NetworkWrapper::CantReadData. All of the network exceptions derive from a common class, so if you don't care about the specific problem, you can just catch NetworkWrapper::NetworkException.

    On the other hand, I have a FileNotFound exception that is global because it makes sense in numerous contexts.

    These aren't absolute rules, of course. It all depends on your situation and organizational style.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  3. Problems with exceptions
    By maneesh in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2005, 05:16 PM
  4. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM
  5. Excaption in Derived Constructors?
    By Davros in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2002, 10:01 AM