don't catch in release?
-
Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;
-
Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;
To catch an error you have to throw an exeption by calling throw. In debug mode certain exeptions are handled by the debuger, but that's not the case in your release version. If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called. This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is: void terminate();
int a = 0;
int b;
try
{
b = 10 / a;
if( a == 0 ) throw "Error"; // You need this
printf("b = %d a = %d\n", b, a);
}
catch (...)
{
printf("catch it");
return -1;
}// Afterall I realized that even my comment lines have bugs
-
Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;
For me, it works in Debug as well as Release build. The message is "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." Did you disable exceptions in the Project settings under the 'C++ Language'-tab?
My opinions may have changed, but not the fact that I am right.
-
To catch an error you have to throw an exeption by calling throw. In debug mode certain exeptions are handled by the debuger, but that's not the case in your release version. If an exception is not caught by any catch statement because there is no catch statement with a matching type, the special function terminate will be called. This function is generally defined so that it terminates the current process immediately showing an "Abnormal termination" error message. Its format is: void terminate();
int a = 0;
int b;
try
{
b = 10 / a;
if( a == 0 ) throw "Error"; // You need this
printf("b = %d a = %d\n", b, a);
}
catch (...)
{
printf("catch it");
return -1;
}// Afterall I realized that even my comment lines have bugs
Toni78 wrote: ...because there is no catch statement with a matching type... But there is: He has written
catch(...)
to catch ALL exceptions!
My opinions may have changed, but not the fact that I am right.
-
Hi! I just try to add some exception procession in my console program. But it seems work in debug but not in release. I don't know what's up. In debug, it output a "catch it" and stop itself. but in release, there is a dialog popup out "Integer Divide by Zero". int a = 0; int b; try { b = 10 / a; printf("b = %d a = %d\n", b, a); } catch (...) { printf("catch it"); return -1; } return 0;
VC6? There is a bug in the optimizer with VC6 that causes simple try/catch blocks from working. What happens is that the compiler thinks there can't be an exception so it optimizes the try/catch block away. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
VC6? There is a bug in the optimizer with VC6 that causes simple try/catch blocks from working. What happens is that the compiler thinks there can't be an exception so it optimizes the try/catch block away. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Toni78 wrote: ...because there is no catch statement with a matching type... But there is: He has written
catch(...)
to catch ALL exceptions!
My opinions may have changed, but not the fact that I am right.
jhwurmbach wrote: But there is: He has written catch(...) to catch ALL exceptions! These are the definitions for try, catch, and throw: "The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type. We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch." You have to throw an exception because you can't just simply expect the compiler to throw every kind of exception that there is out there. Of course you can argue and say that division by zero is a standard exception and I agree with you on that, but catch(...) doesn't catch ALL the ecxeptions unless you throw some of them. Otherwise, programs would never crash. // Afterall I realized that even my comment lines have bugs
-
jhwurmbach wrote: But there is: He has written catch(...) to catch ALL exceptions! These are the definitions for try, catch, and throw: "The code within the try block is executed normally. In case that an exception takes place, this code must use throw keyword and a parameter to throw an exception. The type of the parameter details the exception and can be of any valid type. We can also define a catch block that captures all the exceptions independently of the type used in the call to throw. For that we have to write three points instead of the parameter type and name accepted by catch." You have to throw an exception because you can't just simply expect the compiler to throw every kind of exception that there is out there. Of course you can argue and say that division by zero is a standard exception and I agree with you on that, but catch(...) doesn't catch ALL the ecxeptions unless you throw some of them. Otherwise, programs would never crash. // Afterall I realized that even my comment lines have bugs
;)Pal, sorry. I still not very clear about it. Does it mean we can't catch a "divide by zero" exception at all? We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? Any code to solve my example?
-
For me, it works in Debug as well as Release build. The message is "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." Did you disable exceptions in the Project settings under the 'C++ Language'-tab?
My opinions may have changed, but not the fact that I am right.
yes, Exception is enable in 'C++ Language' as default. I won't like a "First-chance exception in test4.exe: 0xC0000094: Integer Divide by Zero." message. That's just i get in release version. What i want is a console output "catch it" and end the program like the behavior in debug version.
-
;)Pal, sorry. I still not very clear about it. Does it mean we can't catch a "divide by zero" exception at all? We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? Any code to solve my example?
novachen wrote: Does it mean we can't catch a "divide by zero" exception at all? I am not sure how the VC6 compiler handles this particular case so take a look at CException. If you are not using MFC then you have to write your own code. novachen wrote: We have to exam every number whether it is zero before we do a divide if we want the program wont crash with a 0? If you want to handle a divide by zero exception in your own way (not the compilers way) yes you have to check for every number before you divide, and you have to throw the error. novachen wrote: Any code to solve my example? You could check my first reply to your message. // Afterall I realized that even my comment lines have bugs