Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objection to exceptions

One of my friends raised this problem to me. I was stuck because I am not an adept at using exceptions. Keep in mind that we both work in an working environment where we use C++ but do error handling in C tradition. His problem was something like this:

Function A calls B which in turn calls C. An exception is thrown from C and the catch block for that exception is in A. What happens to resources acquired in B before the call to C? How do we clean those up? My answer was use RAII. But even when I said it, I knew it wasn't gonna work. We have huge code bases that have been written in the C mode. Nowhere in code have I seen auto pointers and such. Resources aren't necessarily wrapped up in classes. Even when they are, the destructors are left for the compiler in most cases. In short, everything is done manually.

The real problem is what to do to make the transition from C error handling to exceptions with the weight of a huge code base? The problem my friend asked is just one of the possible questions that can be raised when you have your feet grounded in C error handling and want to know how the migration from there to exceptions can occur.

like image 910
nakiya Avatar asked Feb 11 '11 09:02

nakiya


People also ask

What is an exception in a contract?

In contracts, statutes, and deeds,an exception is a statement that something is not included, as in "Landlord rents to Tenant the first floor, with the exception of the storage room.” To "take exception" to a judge's ruling, is a way a lawyer might tell a judge that they disagree.

What does it mean to except in law?

1) a formal objection during trial ("We take exception, or simply, "exception")" to the ruling of a judge on any matter, including rulings on objections to evidence, to show to a higher court that the lawyer did not agree with the ruling.

How do you get around hearsay objection?

Even if an utterance contains a factual assertion, it is only hearsay if the evidence is offered to prove the truth of that factual assertion. You can therefore respond to a hearsay objection by arguing that the statement helps prove a material fact other than the fact asserted in the statement.

What is the meaning of objection overruled?

"Objection overruled", a statement by a judge who disagrees with a formal protest raised in court during a trial.


2 Answers

There is a technique developed by Andrei Alexandrescu and Joshua Lehrer called Scope Guard which provides a technique for providing a "scope-exit" code as soon as you allocate an object, and opening the scope for it at that point.

The D programming language actually has it as standard.

boost libraries has an enhancement of this called Scope Exit. If your code is not exception-safe, as function B appears not to be, it will not handle properly if anything throws.

Destructors will run when unwinding the call-stack after an exception. That is why you must ensure they do not themselves throw any exceptions.

like image 163
CashCow Avatar answered Sep 22 '22 19:09

CashCow


The real problem is what to do to make the transition from C error handling to exceptions with the weight of a huge code base?

No, the real problem is that, since you're programming in C++, you should have long since be done with this. Even if your own code does not throw exceptions, some code in the standard library and runtime system might throw (new, dynamic_cast) and, of course, any 3rd-party software you use might throw was well.

I agree that Scope Guard is probably your best bet to add exception-safety as an afterthought. However, don't fool yourself by thinking you don't need to do that as long as you don't throw exceptions yourself. You are using a language with exceptions, so you'd better start dealing with them as soon as possible.

like image 27
sbi Avatar answered Sep 22 '22 19:09

sbi