-
March 23rd, 2010, 08:47 PM
#1
How to print (OR cout rather) custom error messages?
assert() is a good way to notify errors but I want to cout an elaborate message.
Of course the following method will work but I needed something better:
if(error){
cout<<"Elaborate message";
assert(0);
}
Good tips anyone?
-
March 24th, 2010, 03:01 AM
#2
Re: How to print (OR cout rather) custom error messages?
Of course the following method will work but I needed something better:
Better in what way ?
-
March 24th, 2010, 03:08 AM
#3
Re: How to print (OR cout rather) custom error messages?
From your description, it's impossible to know what you want.
-
March 24th, 2010, 09:16 AM
#4
Re: How to print (OR cout rather) custom error messages?
My apologies.
Here's the actual code:
Code:
void temp_match_img::makevec(){
if(radpix_set==0) cout<<"The values of the radpix[] array have not yet been calculated!\n";
assert(radpix_set);
int temp;
for(int i=-radius; i<=radius; i++){
for(int j=-radius; j<=radius; j++){
temp=(int)sqrt(float(i*i+j*j));
if(temp<=radius){
rpvec[temp]+=tmimg[i+row][j+col]/radpix[temp];
}
}
}
}
What you can see in bold are 2 statements. What I wanted was to replace those 2 statements by say:
assert_new(radpix_set, "The values of the radpix[] array have not yet been calculated!\n");
Any better method is also appreciated. The problem is that assert prints a lot of stuff that I am not interested in. I can print my own stuff (as can be seen in the code) but the message of assert takes some space. Please let me know if you know a way around.
Regards.
-
March 24th, 2010, 09:45 AM
#5
Re: How to print (OR cout rather) custom error messages?
So what you want is an assert window with your own message? Well, you can create such a custom window in your project and display it when you need it instead of assert().
-
March 24th, 2010, 01:07 PM
#6
Re: How to print (OR cout rather) custom error messages?
An assert kills the program, so trying to format it well isn't all that useful; the only time you should legitimately encounter it is during debugging, and your debugger will stop at the assertion failure anyway, so you can examine the variables directly.
If you're looking to report a failure on the user's part in a meaningful way, consider using an exception (perhaps throwing a std::runtime_error) instead.
Actually, throwing a std::logic_error is probably more useful than an assert even for debugging, since you can attach a message to it. If it isn't caught, that message gets dumped to the screen.
-
March 24th, 2010, 01:35 PM
#7
Re: How to print (OR cout rather) custom error messages?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|