Ok I have been using return codes for a very long time.
I didnt initialized objects in constructors because they dont have return codes.
Instead I created a method like bool create(parameters) to create the object.
And I destructed the object in the destructor.
Ok here is an example of what i have at the moment.
bool DAGameApp::initApplication(HINSTANCE hInstance)
{
if(DialogBox(hInstance,MAKEINTRESOURCE(IDD_OPTIONS),NULL,(DLGPROC)dlgOptionsProc)==0)
return false;
if(!registerWindowClass("DAGameClass",hInstance,DAGameWindow::WndProc,loadIcon(IDI_APPLICATION),STWBACK_BLACK))
return false;
m_MainWnd.reset(new DAGameWindow);
if(!m_MainWnd)
return false;
//Set creation params
m_MainWnd->setWidth(g_InitInfo.iWidth);
m_MainWnd->setHeight(g_InitInfo.iHeight);
m_MainWnd->setFullScreen(g_InitInfo.bFullscreen);
m_MainWnd->setBits(g_InitInfo.iBits);
DWORD dwStyle = WS_VISIBLE;
if(g_InitInfo.bFullscreen)
dwStyle|=FULLSCREEN;
else
dwStyle|=WINDOWED;
//Create the game window
if(!m_MainWnd->create("DAGameClass","Dark Age v0.3",NULL,NULL,dwStyle))
return false;
//if the window creation succeded create the game objects
if(!m_MainWnd->createGameObjects())
return false;
//Hide the cursor if necessary
if(g_InitInfo.bFullscreen)
ShowCursor(FALSE);
return true;
}
m_MainWnd is a boost::shared_ptr.As you can see I have lots of ugly if statements for each method.I think i can get rid of these if I switch to exceptions.So I derived a class from std::runtime_error:
namespace STE
{
class STException : public std::runtime_error
{
public:
STException(const std::string& error):std::runtime_error(error){}
private:
};
}
And i threw from my library functions when they fail here is the registerWindowClass method from above :
void WndApp::registerWindowClass(char *szClass,HINSTANCE hInstance,WNDPROC WndProc,HICON hIcon,int iBackground)
{
m_hInstance = hInstance;
WNDCLASSEX wndClass;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = hIcon;
wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(iBackground+1);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szClass;
wndClass.hIconSm = hIcon;
if (!RegisterClassEx(&wndClass)){
WRITETOLOG("Window class register failed.");
throw STE::STException("Window class register failed!");
}
}
My question is,is it ok to catch all the exceptions in the WinMain like this :
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
try
{
STE::Log::createFile("logGame.txt");
WRITETOLOG("Dark Age Started!");
int loopResult = 0;
boost::shared_ptr<DAGameApp> gameApp(new DAGameApp);
loopResult = (int)gameApp->messageLoop(hInstance);
}
catch(const STE::STException& e)
{
MessageBox(NULL,"dd","dd",MB_OK);
}
WRITETOLOG("Dark Age Ended!");
_CrtDumpMemoryLeaks();
return loopResult;
}
With this I will only have one try catch.
1)What is the disadvantage of this?
2)STException is derived from std::runtime_error.Will it catch all runtime_exceptions? or do i need another catch(std::runtime&)
3)When i throw a exception messageBox crashes.Any idea why?
(Nevermind this. I figured out why it was crashing.I wasnt calling DestroyWindow in the window class destructor.Adding it to the destructor fixed it.)
Sorry for the long post :P
[Edited by - Black Knight on June 25, 2008 2:18:06 PM]