assert VS exceptions [modified]
-
hi all :) I saw this code :
inline int foo (char* pBuffer, const char* formatString) throw() {
assert (pBuffer);
assert (formatString);return "something";
}If i use this code and in
Release
build and passpBuffer
, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :inline int foo (char* buffer, const char* formatString) {
if(NULL == buffer)
throw someException();if(NULL == formatString);
throw someOtherException();return "something";
}In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use
asserts
and notthrows
? thank you -- modified at 4:01 Monday 29th May, 2006 -
hi all :) I saw this code :
inline int foo (char* pBuffer, const char* formatString) throw() {
assert (pBuffer);
assert (formatString);return "something";
}If i use this code and in
Release
build and passpBuffer
, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :inline int foo (char* buffer, const char* formatString) {
if(NULL == buffer)
throw someException();if(NULL == formatString);
throw someOtherException();return "something";
}In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use
asserts
and notthrows
? thank you -- modified at 4:01 Monday 29th May, 2006Programming errors are not exceptions. You can't remove bugs until and unless you fix them. Once you have found and fixed all of the bugs,the assert macros are no longer necessary and you should turn them off by defining the NDEBUG macro. Assertions show that you are still testing and debugging your code. An exception is an unpredictable but expected event which cannot be prevented and must be handled. Exceptions should be handled to the extent possible at the point where they are first detected. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
Programming errors are not exceptions. You can't remove bugs until and unless you fix them. Once you have found and fixed all of the bugs,the assert macros are no longer necessary and you should turn them off by defining the NDEBUG macro. Assertions show that you are still testing and debugging your code. An exception is an unpredictable but expected event which cannot be prevented and must be handled. Exceptions should be handled to the extent possible at the point where they are first detected. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
_AnShUmAn_ wrote:
Once you have found and fixed all of the bugs
How can you know that you have eliminated all bugs ?
_AnShUmAn_ wrote:
Assertions show that you are still testing and debugging your code
I see asserts in already posted codes...
_AnShUmAn_ wrote:
An exception is an unpredictable but expected event
:confused:, why unpredictable. Can not understang it fully :( anyway thanks for reply
-
hi all :) I saw this code :
inline int foo (char* pBuffer, const char* formatString) throw() {
assert (pBuffer);
assert (formatString);return "something";
}If i use this code and in
Release
build and passpBuffer
, whose value will be NULL, than I get a bug yes ? these asserts are useful only in debug mode, not ? Is not it better to above this function like this :inline int foo (char* buffer, const char* formatString) {
if(NULL == buffer)
throw someException();if(NULL == formatString);
throw someOtherException();return "something";
}In this case if we catch the thrown exception than, we are secure in both Release ana Debug builds, not ? so why use
asserts
and notthrows
? thank you -- modified at 4:01 Monday 29th May, 2006asserts are using only for debug pupose. suppose if ur buffer getting NULL a endless loop or a lengthy looop, u need not to debug the each loops. if an assertion occrurs then it will show "This expression failed at this line of source code" then u can trace and correct the bug. never put ur logic inside the assert macro. because it wont work in release build. in release build VERIFY macro will satisfy u with the same functionality of assert offer. I think asserts and exception standing at different areas of erro handling. We are used to compare with strctured error handling and the normal error handling we are used to follow in "C". it is depends on ur system to select which is suitable. each mechanism has its own advantages and disadvantages. SaRath
-
_AnShUmAn_ wrote:
Once you have found and fixed all of the bugs
How can you know that you have eliminated all bugs ?
_AnShUmAn_ wrote:
Assertions show that you are still testing and debugging your code
I see asserts in already posted codes...
_AnShUmAn_ wrote:
An exception is an unpredictable but expected event
:confused:, why unpredictable. Can not understang it fully :( anyway thanks for reply
if the application is performing as per your expectations with no errors or CRASHES you should know that all the bugs are fixed. It's that simple. So when you are done with this disable all ASSERTS by defining nDEBUG macro. Exceptions are unpredictable because in some applications if many threads are running simultaneously than there may be a variable that would get modified in the other sections and cause an exception in another block but this is not guaranteed . It depends on the time the system allocates to each thread. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_