W
William Payne
Hello, I am making a binary tree and I've encountered a problem when I throw
an exception. Consider this code:
/* in inserter function for the tree */
char error_message[64];
sprintf(error_message, "%i already present in the tree.", n); /* n is of
type int */
cout << error_message << endl;
throw error_message;
/* in main() */
try
{
binary_tree.insert(7);
}
catch(const char* error_message)
{
cerr << error_message << endl;
}
When I print out the string I am about to throw in the inserter-function, I
get:
7 already present in the tree.
but when I catch the exception in main() and print out I get:
? al(?C y present in the tre"í
As you can see, it has become corrupted. What am I doing wrong? Must I
allocate the error message dynamically or?
/ WP
an exception. Consider this code:
/* in inserter function for the tree */
char error_message[64];
sprintf(error_message, "%i already present in the tree.", n); /* n is of
type int */
cout << error_message << endl;
throw error_message;
/* in main() */
try
{
binary_tree.insert(7);
}
catch(const char* error_message)
{
cerr << error_message << endl;
}
When I print out the string I am about to throw in the inserter-function, I
get:
7 already present in the tree.
but when I catch the exception in main() and print out I get:
? al(?C y present in the tre"í
As you can see, it has become corrupted. What am I doing wrong? Must I
allocate the error message dynamically or?
/ WP