CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2008
    Posts
    44

    Exception handling

    Dear friends, I´m triyng to get familiarized with the expression “try / catch” and it seems to be clear for me how to catch an error and display a message with this error that stops the program.

    However…..

    What is the method to avoid message errors and allow the normal flow of the program when the program has to perform thousands or millions of calculations and some of them results in an overflow or div0 error?

    I would really appreciate your help.

    Regards

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Exception handling

    Don't display errors in your catch block?

    I'm not really sure what the problem is. If you know how to display errors, I would imagine you know how not to display them.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Exception handling

    I don't think try/catch will typically do anything for div0 or overflows. It will catch exceptions, which are not errors per se but rather are entities within your program which are invoked when the normal course of logic just won't work for whatever reason. The key thing about exceptions is that they must be deliberately thrown----you won't get one just because the machine tried to execute an instruction and couldn't.

    That means you either catch exceptions which are documented to be possible by the library you're using, or you catch exceptions which you throw in your own code.

    More generic issues like overflows must be avoided by other means.

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

    Re: Exception handling

    What is the method to avoid message errors and allow the normal flow of the program when the program has to perform thousands or millions of calculations and some of them results in an overflow or div0 error?
    Error catching is a last resort, not a mechanism to hide mistakes. If you have a division by zero in your code, you simply need to fix your code.

  5. #5
    Join Date
    May 2008
    Posts
    44

    Re: Exception handling

    Hi friends, I'm still stuck with this, so please let me point to the real nature of my problem.

    I am developing a program that iterates mathematical expressions in the complex plane (Looking for fractals) The procedure to do this usually implies to perform billions of calculations with many different formulas and usually involving in these calculations every screen pixel converted to its equivalent complex coordinates.

    Mi code calls mathematical functions (sin, cos, tan, pow...etc) included in a library called <complex> to submit every pixel to some calculations and depending on the result tries to set the pixel colour. (Black if try fails)

    The problem is that some times and only with some few pixels the program returns error messages generated probably by this library.

    So, how can I take the control of these messages to avoid them and allow the normal flow of the program?

    I mean, if calculations for one pixels is not possible the program should continue with the following without showing a message error.

    Regards

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Exception handling

    Returning an error message and throwing an exception aren't really the same. Until we know how the library indicates an error and how that error is getting displayed, there's nothing anybody here can do for you.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Exception handling

    Quote Originally Posted by jgabase View Post
    The problem is that some times and only with some few pixels the program returns error messages generated probably by this library.
    Why not verify where these error messages are generated first?
    So, how can I take the control of these messages to avoid them and allow the normal flow of the program?
    I don't know what the "<complex>" library is, but if it's a third-party library, then either read the documentation for this library, or contact the authors and ask them how to turn off the error messages.
    I mean, if calculations for one pixels is not possible the program should continue with the following without showing a message error.
    Error messages are generated by the code in the library calling some function to show the error messages. You can't stop this unless an option is set in the library, or some other function(s) in the library stops them and you need to call these functions, or a library config setting needs to be set, etc.

    As GCDEF stated, no one can help you here. You are using this <complex> library, therefore you should be well aware of how to use it, and that comes by reading the documentation for the library's functions, or contacting the author(s) of the library to help you.

    Regards,

    Paul McKenzie

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Exception handling

    There is http://www.cplusplus.com/reference/std/complex/ in the standard library. Is that what you're using?

  9. #9
    Join Date
    May 2008
    Posts
    44

    Re: Exception handling

    Yes, this library comes with my IDE and I just use:

    #include <complex>

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Exception handling

    Quote Originally Posted by jgabase View Post
    Yes, this library comes with my IDE and I just use:

    #include <complex>
    Some clarification to your terminology:

    The <complex> is a standard header file, just like <iostream>, <cstdio>, etc. are standard headers.

    Second, all ANSI compliant C++ compilers must provide this header file.

    Third, it also isn't part of the IDE -- it is part of the compiler itself. All the IDE does is give you a central place to do editing, compiling, linking, and debugging -- the IDE is not the compiler or linker -- those are separate programs that are launched by the IDE when asked to build a program.

    But let's cut to the chase. What happens when you do this:
    Code:
    int main()
    {
       double x = 10.0;
       doubley y = 0.0;
       double z = x / y;
    }
    Do you see an error message generated? If you do, then the issue is not <complex>, as you can see, there is no <complex> header used. The issue will be the Floating point processor (FPU) is set to detect and report divide by zero.

    Before suggesting anything to you, please verify that code that doesn't use the routines in <complex> also generate the same messages by compiling and running the program above.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    May 2008
    Posts
    44

    Re: Exception handling

    Hi Paul,

    Your code generates the message error “Floating point division by zero”

    However, to perform operations with complex numbers it is necessary to declare it as complex variables.

    double xrealpart=10, ximaginarypart=10;
    double yrealpart=0, yimaginarypart=0;
    double zrealpart, zimaginarypart;

    complex <double> x ( xrealpart , ximaginarypart);
    complex <double> y ( yrealpart , yimaginarypart);
    complex <double> z ( zrealpart , zimaginarypart);

    z=x/y;

    // After that, if you want to extract the real and imaginary part you can use..

    zrealpart = real(z);
    zimaginarypart = imag(z);

    This code works fine unless “yrealpart” and “yimaginarypart” are zero at the same time. In this case you will obtain the message error “Invalid floating point operation”


    In this other case..

    double xrealpart, ximaginarypart;
    double yrealpart, yimaginarypart;

    complex <double> x ( xrealpart , ximaginarypart);
    complex <double> y ( yrealpart , yimaginarypart);

    y = sin(pow(x,-1.5));

    For some few values of “xrealpart” and “ximaginarypart” I,m getting the messages

    “sinh: OVERFLOW error”
    “cosh: OVERFLOW error”

    These errors are what I want to avoid telling the program to skip any message, so that the application can continue without stopping.

    Regards
    Last edited by jgabase; September 4th, 2011 at 06:15 AM. Reason: Code error

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Exception handling

    Quote Originally Posted by jgabase View Post
    Hi Paul,

    Your code generates the message error “Floating point division by zero”
    These errors are what I want to avoid telling the program to skip any message, so that the application can continue without stopping.
    I'll quote myself again:
    The issue will be the Floating point processor (FPU) is set to detect and report divide by zero.
    So have you done any research as to how to set the FPU to not report these errors? Did you investigate _controlfp() or _control87() or how to set the FPU registers to not report these errors?

    Secondly, regardless if you set the processor to report or not report, you will have invalid numbers being processed, and you would never know it, since the reporting mechanism in the FPU has been turned off. It is your responsibility to check for invalid values before sending them to these mathematical functions. If the values are bad, then you should do something with these bad values. So what do you do with these bad values?

    Regards,

    Paul McKenzie

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
  •  





Click Here to Expand Forum to Full Width

Featured