c++ - Exception handling and recovering from non-fatal errors -
i'm writing c++ application needs deal error codes c api, winsock2 api. i'm not sure of best way handle fatal errors vs non-fatal errors.
for example, if phone call send()
fails error code wsaeconnaborted
, want homecoming error caller, go on programme execution. in case caller mark connection terminated , carry on. if fails error i'd consider fatal, such wsaenetdown
, want application terminate.
i know exceptions used "exceptional" situations, makes sense utilize exceptions handle fatal error case. i'm questioning how handle non-fatal error cases.
should homecoming error code in non-fatal cases? if need homecoming value, i'll need homecoming both code (potentially success) , value. or should create 2 exception classifications, fatalerror
, nonfatalerror
classes, , effort recovery when handling nonfatalerror
exceptions? or there other/better ways this?
[updated] clarification there seems confusion i'm getting @ original question. understand how exceptions handled, happens when they're not, rethrowing exceptions, , performance characteristics.
i'm asking techniques/best practices handle situations aren't "exceptional". in case, errors may occur cause connection become unusable, not impact rest of server.
preferably i'd avoid having exception class per error code, since create lot of useless exception classes.
you can utilize exceptions if want propagate error callstack. create custom classes, inherit std::exception, , grab them if want apply specific treatment.
try { my_function_which_throws(); } grab (const fatalerror& e) { // something, or rethrow } grab (const nonfatalerror& e) { // log or nil }
exceptions take time handle if thrown. if function don't throw anything, won't impact performance.
c++ design exception-handling
No comments:
Post a Comment