Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need help with try and catch blocks

Started by Smacker_626 Mar 5, 2005 at 1:23 AM 5 replies 1.3k views
Original Post
Smacker_626
Smacker_626
I just started to use try and catch blocks for my new program. I made a class called CException that holds a long and 3 std::string's (Error Msg, Line, Filename, Function Name). I have the code like this

try
{
  // THROW_STR is a macro in CException.h along with THROW_NUM and THROW
  THROW_STR("THIS IS A TEST :P");
}
catch(CException e)
{
  // e.Format().c_str() wont work in the call to AddLog for some reason
  // Format returns a std::string with everything in it ;)
  string strError = e.Format();
  // AddLog of course adds the text to the log
  AddLog("\nException thrown!\n%s", strError.c_str());
}

ProgCleanup();
return g_iExitCode;

The compiler gives an error msg for THROW_STR(); "expected primary-expression before '(' token", I was going to say it gives an error msg for catch(CException e) too but I hit rebuild all again while I was writing this and it worked :-/. Also if I comment out the try / catch blocks and put

CException e("TEST", -1, __FILE__, __LINE__, __FUNCTION__);
AddLog("\nException thrown!\n%s", e.Format().c_str());

before them it gives me these errors "[Linker error] undefined reference to CException::CException(std::string, long, std::string, long, std::string)'" "[Linker error] undefined reference to CException::Format()'" "[Linker error] undefined reference to CException::~CException()'" "[Linker error] undefined reference to CException::~CException()'" yes it repeated that one twice and when I just use

try
{
  throw "TEST";
}
catch(string s)
{
  string strError = s;
  AddLog("\nException thrown!\n%s", strError.c_str());
}

ProgCleanup();
return g_iExitCode;

it won't put that text in the log but the text from ProgCleanup(); gets put in :-/, any help would be really nice :D I RARed and uploaded my code to > http://www.dragonruins.com/MyCode.rar You need v2.9 or > to UnRAR it (WinRAR - http://www.rarlabs.com/ of course :) ) I'm using Dev-CPP v4.9.9.1 - http://www.bloodshed.net/ (Use's gcc as the compiler) to compile everything. And how do you get URL's to work? Thanks ;-)
Smacker_626
Smacker_626
Yes lol, didn't think of saying that.
Zahlman
Zahlman
    CException(string strError="", long lError=-1, string strFilename="", long lLine=-1, string strFunction="");//...#define THROW_STR(strError)     throw CException(strError, , __FILE__, __LINE__, __FUNCTION__)#define THROW_NUM(lError)       throw CException( , lError, __FILE__, __LINE__, __FUNCTION__)


Sorry, default arguments don't work like that. You'll have to fill the default values in in the macros.

As for the linker error, I suspect it's because CException.cpp didn't compile and you somehow didn't notice:

strTemp += "\n Error Number: " + strErrorNum; // shouldn't that be 'm_strError'?


I could criticize your style a whole bunch too but I think I'll let you fix the problems first :)
Smacker_626
Smacker_626
Thanks Zahlman (you to Doc ;) ), it works now.

CException::Format() was all messed up :|, I use to use ltoa in there but I compiled the program with strTemp += x + theNum and it worked so I figured I would take ltoa out and I got all my variable names mixed up in there lol.

And you were right about CException.cpp not being compiled, I had it like that for awhile so I could compile my program w/o errors until I could rewrite the class.

And you were right about my macros too, you can still leave the values out as long as you don't specify the values for any of the arguments after that one right?

And Zahlman what do you mean about my style? :(, i'm trying my best.

I rated both of you A+ :), thx for your help.

EDIT: No need for the code on my site anymore so i'm taking it down.
Qw3r7yU10p!
Qw3r7yU10p!
you would usually want to catch by reference rather than by value.
Smacker_626
Smacker_626
Thx petewood, fixed something else for me :)

Like I said though i've never used try / catch blocks before so I didn't know :P, I rated you too :)

(Maybe it would of been best if I hadn't finally realized that "Rate This User" was at the bottom of every post :P)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.