How to deal with unchecked exceptions?

Java has a compiler that checks for exceptions . When I switched to C ++, I found out that it does not contain checked exceptions. At first, I continued to use exception handling because it is a great feature. However, after some time I refused it, because I got into the situation, every function could throw an exception. Since only a small percentage of the functions I write can throw exceptions (say no more than 25%), I found the overhead of handling exceptions for functions that cannot throw anything unacceptable.

Because of this, I am really surprised that there are many developers who prefer excluded exceptions. So I'm curious to know how they deal with this problem. How to avoid the overhead of doing unnecessary exception handling if the language does not support checked exceptions?

Note: My question applies equally to C ++ and C #, and possibly to all other languages ​​that do not implement exception handling using the compiler.

+5
source share
4 answers

#, . , . , - ... - , - .

, Java, , . , - , , .

, , "", # Java.

+11

Simple. " , " - ++ . , , , RAII ( ) .

+16

, , , try/finally ( ++ try/catch/throw), - , .

try.

+4

For C ++ in particular, the overhead goes pretty much if you design your classes well and use RAII.

Martin York wrote a wonderful example of this in this answer .

A function may throw an exception, yes, but if that happens, nothing special is needed to clear it. Thus, you only need to actually catch the exception in one place - a function that can handle it and recover from the error.

+3
source

All Articles