CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    27

    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?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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 ?

  3. #3
    cilu's Avatar
    cilu is offline Moderator/Reviewer/MS MVP Power Poster cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+)
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,349

    Re: How to print (OR cout rather) custom error messages?

    From your description, it's impossible to know what you want.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Apr 2009
    Posts
    27

    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.

  5. #5
    cilu's Avatar
    cilu is offline Moderator/Reviewer/MS MVP Power Poster cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+) cilu has a reputation beyond repute (3000+)
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,349

    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().
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Lindley is offline Elite Member Power Poster Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+) Lindley has much to be proud of (1500+)
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,861

    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.

  7. #7
    Join Date
    Apr 2009
    Posts
    27

    Thumbs up Re: How to print (OR cout rather) custom error messages?

    Perfect!

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width