vrbilgi
vrbilgi

Reputation: 5803

How do I force compiler to generate error or warning when I forget to catch an exception

If forgot to catch exception, It will return to the runtime or handled by Exception Handler function defined. or I can wrap the code in main in try catch. Both will handle the exception in decent way.

But I want detect this situation in early stage, Is there is any way do that.

Upvotes: 1

Views: 1070

Answers (3)

curiousguy
curiousguy

Reputation: 8318

But I want detect this situation in early stage, Is there is any way do that.

If you want to handle errors locally, you should not use exceptions.

Upvotes: 0

thiton
thiton

Reputation: 36059

C++ does not support Java-style restrictive exception declarations, and cannot do it for a number of reasons. Your compiler can't and won't warn you about exceptions that were not caught. The C++ throw() specifications serve a different purpose (or, arguably, no purpose at all at the moment).

The standard solution is to make sure all exceptions derive from stdexcept classes and to catch std::exception or std::runtime_error at an appropriate place. This normally means a very high-level place, e.g. the controller function that activated an action.

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 474436

C++ (thankfully) does not require functions to advertize what exceptions they might throw. Therefore, in general, any function may throw anything. You don't know and your compiler doesn't know.

Therefore, you have to take it upon yourself to track what exceptions get thrown from where, and where they get caught.

Upvotes: 2

Related Questions