Re:
by qa_acc on Friday August 25, 2023 @02:33PM
(#37265471)
Hi, professional C++ developer here, I find this post casually and, for curiosity, I watched the video. The example used by the guy is wrong beyond recovery and I'm about to detail why after the premise that, yes, exception are slow:
1. exceptions have that name because are intended to handle event out of normal , error and not to returns value or information to the caller. No skilled C++ developer will write functions like those, never !
2. In that kind of function exception are useless, in fact that should be write like this, with exception disabled:
int calc(int a, int b) noexcept {
if(... omissis ...) return 1;
return 0;
}
3. In case the function had some more complex errors, the correct form for the use of exceptions should be:
int calc(int a, int b) {
try{
// More complex logic here
if(... omissis ...) return 1;
return 0;
} catch(myexcept& ex){
// do something
// possibly other catch for different except here
} catch(...){
// do something
}
}
So, not to return information but for error handling.
3. From a skilled developer , from a financial company, I expected a real life example , with correct exception use and a real case in which their presence can slow down a critical section of a program. That could be an interesting lesson;
4. Moreover, the examples are particularly bad because could induce people to think that in other contexts, where performance are not so important, the use of the exceptions in that way is justifiable ! I guess that could be a reason the article was downvoted.
1. exceptions have that name because are intended to handle event out of normal , error and not to returns value or information to the caller. No skilled C++ developer will write functions like those, never !
2. In that kind of function exception are useless, in fact that should be write like this, with exception disabled:
int calc(int a, int b) noexcept {
if(... omissis ...) return 1; return 0;
}
3. In case the function had some more complex errors, the correct form for the use of exceptions should be:
int calc(int a, int b) {
try{
// More complex logic here
if(... omissis ...) return 1; return 0;
} catch(myexcept& ex){ // do something // possibly other catch for different except here } catch(...){ // do something }
}
So, not to return information but for error handling.
3. From a skilled developer , from a financial company, I expected a real life example , with correct exception use and a real case in which their presence can slow down a critical section of a program. That could be an interesting lesson;
4. Moreover, the examples are particularly bad because could induce people to think that in other contexts, where performance are not so important, the use of the exceptions in that way is justifiable ! I guess that could be a reason the article was downvoted.