RunTimeError() in C++?

Community Forums/General Help/RunTimeError() in C++?

JoshK(Posted 2010) [#1]
Is there any simple mechanism to generate an error in C++, besides Assert and Try/Catch?


N(Posted 2010) [#2]
RunTimeError in BlitzMax is just throwing an exception, so you could throw an exception. Alternatively, you can do like Cocoa does and provide something like NSError** as an optional argument to a method (if errors can be returned). There's always the tried and true fprintf(stderr, "%s\n", errorMessage); exit(1); if you just want to kill the program.

There are a lot of methods for generating/handling errors. In C, when using the Clang compiler and enabling blocks, you can do something like continuation-passing, so if Clang ever supports C++ somewhat fully, that will probably come for free as well.


markcw(Posted 2010) [#3]
I use printf for debugging. I know it's messy but it's mostly all I need. I think assert is the C++ equivalent to RunTimeError.

Also, I just found this tutorial so I thought I'd share.
http://newdata.box.sk/bx/c/index.htm


JoshK(Posted 2010) [#4]
throw requires a structure of some kind...MS docs aren't very clear.


N(Posted 2010) [#5]
Also, I just found this tutorial so I thought I'd share.
That tutorial looks horrible. The code wouldn't even compile (looking at it even briefly should make this obvious). Why would you want to subject yourself, or anyone else, to something that teaches you using broken code?


Brucey(Posted 2010) [#6]
assert? exit(1)?

Yes. I also vote for killing the program on error. For obvious reasons - like user-friendliness.


JoshK(Posted 2010) [#7]
Well, the reason I ask is I am testing Lua debugging, and need a way to induce bugs.


markcw(Posted 2010) [#8]
That tutorial looks horrible. The code wouldn't even compile (looking at it even briefly should make this obvious). Why would you want to subject yourself, or anyone else, to something that teaches you using broken code?

Do you mean it's borked because of the line numbers? It looks okay to me but I'd only skim over it and probably wouldn't actually compile any code.


*(Posted 2010) [#9]
cerr << "My Error message" << endl;
exit( 1 );

is what I use :)


Canardian(Posted 2010) [#10]
void RunTimeError(char *message, int severity=2)
{
	if(severity>0)
		if(severity>1)
			printf("ERROR: %s\n",message);
		else
			printf("WARNING: %s\n",message);
	else
		printf("INFO: %s\n",message);
	exit(severity);
}



Azathoth(Posted 2010) [#11]
C++ throw can throw anything, but usually its best to throw instances of classes that inherit from std::exception.

www.parashift.com/c++-faq-lite/exceptions.html#faq-17.6


D4NM4N(Posted 2010) [#12]
Afaik you have to throw if you want anything "special" to happen.

I wish it were more like C# on that front.