0

Most people say that it is costlier to throw exceptions than just returning from a function.

In this article in part 3, author gives an example of mimicking error handling with goto in C (example E1). Later in the same section he gives the equivalent error handling using try-catch in C++ (example T1). I read that if-else is replaced by goto when translated to machine language. If it the same case with try-catch then it would not be costlier. So what makes try-catch flow costlier than normal control flow.

Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
  • On x86, returning a value consists of moving the return value into the return register (~`%rax`), shifting back the stack frame, and moving the instruction pointer. Generally the compiler finds a way to have the result be in the return register by the end of the function so we can ignore that step. The other two steps are completed by a single byte instruction (`ret`). At this point, literally anything else is costlier than just returning the value normally. – Locke Feb 15 '21 at 11:43
  • Does the register hold entire return value? If so, then how can it hold a value of a big struct? (eg: a struct having 100 integers declared). – Sourav Kannantha B Feb 15 '21 at 11:47
  • Due to x86 calling conventions, if a value is too large it gets pushed onto the stack. The same goes for the arguments in function calls. – Locke Feb 15 '21 at 11:48
  • 2
    I think there is a huge misunderstanding regarding exception throwing cost. Even if throwing is rather expensive it is not supposed to happen often (and certainly should not be used as an alternative to normal control flow) while cost of returning and inspecting values on each stack level accumulates all the time making exceptions much faster overall. – user7860670 Feb 15 '21 at 12:02
  • Why don't you profile it and see? For all I know, exceptions have been optimized since everyone was told how slow they were. At least then you'd have some concrete code to compare, and you'd be able to see the generated assembly too. – Useless Feb 15 '21 at 12:04
  • @Useless I am not asking 'are exceptions are costlier?' Instead I am asking why it is so. How a profiling can help for this? – Sourav Kannantha B Feb 15 '21 at 12:05
  • @user7860670 It is true that, when there is no exception, it is zero cost path. But when an exception occurs, it is [much costlier](https://stackoverflow.com/a/13836329/12357035) than if checks. So if the chances of occurring an exception is low, it is better to use exception handling. But if it more frequent, then its debatable. – Sourav Kannantha B Feb 15 '21 at 12:09
  • If you're asking why a _quality-of-implementation_ detail is true, it's important to know both the implementation and the code it's compiling. You making some effort to verify that the claim is true _on your implementation_ would both demonstrate some minimal effort, tell us the implementation, _and_ provide the answer. There isn't a more general one (the duplicate linked describes the "main model used today" - 8 years ago!) – Useless Feb 15 '21 at 12:11
  • 1
    If an "exception" happens frequently than it is not an exception, but rather a normal situation. – user7860670 Feb 15 '21 at 12:11
  • 1
    You are comparing 2 different things. (1) Just returning a result, not doing anything with it and no recovery scheme; with (2) A mechanism to force the caller to deal with unexpected states or have the program terminate. As run-on errors (where the program continues well past the original error site) are very costly to find and debug I prefer (2). – Richard Critten Feb 15 '21 at 12:15
  • @RichardCritten sure. But I am asking how they are different. Both returning and throwing leads to unwinding of stack. Then in the caller function either they have to assign the return value or handle the thrown exception. Then also throwing is costlier than returning. Why so? – Sourav Kannantha B Feb 15 '21 at 12:19
  • This might be helpful: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0709r4.pdf at least section `Problem description, and root causes`. – Marek R Feb 15 '21 at 12:39
  • @MarekR In that report, author says 'Many projects ban exceptions'. Here, for those projects, I think the alternative is propagating error codes through return values. But still they have to be checked on every call of the function. So won't that checking consume some time? – Sourav Kannantha B Feb 15 '21 at 14:41
  • @MarekR Suppose a hypothetical language provides concise and cleaner syntax to write error-code base handling style, then which one would you prefer? error-code or exceptions? – Sourav Kannantha B Feb 15 '21 at 14:59
  • Re: "`if-else` is replaced by `goto`" -- not really. Both `if-else` and `goto` generate code that does a jump. So they're similar, in a very weak sense, but by no means equivalent. – Pete Becker Feb 15 '21 at 16:05

0 Answers0