. .

Exception class with printf-like parameters for C++

When raising exceptions it is good to make their error texts as detailed as possible. Usually this isn’t an easy task, cause it involves type conversions and string operations. This is why I’ve implemented this class, which can be instantiated by printf-like parameters as easily as this line:

throw(new FException("Invalid tag: '%s'. At line: %d.", tag, line));

How it works: the constructors accept a variable argument list and pass it to a vsprintf_s function.

The source code can be downloaded here:

Detailed example:

#include <stdio.h>
#include "exception.h"
 
void test_function(int a, char *c);
 
int main() {
	int a;
 
	try {
 
		a = 5 + 6;
 
		test_function(a, "even");
 
	} catch (FException *ex) {
		ex->Print();
		delete ex;
	}
 
	return 0;
}
 
void test_function(int a, char *c) {
	a++;
 
	throw(new FException("The number %d is %s.", a, c));
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>