9

I am trying to compile simple code with -fno-exceptions flag. Its giving error.

Please let me know how to suppress this. I am using gcc version 4.6.3

Code

#include <iostream>
using namespace std;
int main () {
  try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
  }
  return 0;
}

Log

> g++ throw.cc -o out -fno-exceptions
throw.cc: In function ‘int main()’:
throw.cc:10:11: error: exception handling disabled, use -fexceptions to enable
throw.cc:14:56: error: ‘e’ was not declared in this scope

Edit

I have a client code which have lot throws like this. I have to integrate this in my project and I can't control compilation flags to build(which will come from config which has -fno-exceptions enabled). I wanted a quick work around that I can suggest.

EDIT

I found a workaround see below for my answer.

Kiran
  • 499
  • 2
  • 6
  • 17
  • 7
    Why did you compile with `no-exceptions` when your code is clearly designed to catch an exception? Why don't you follow the compiler's suggestion, and change to `-fexceptions`? – abelenky Aug 19 '15 at 11:47
  • 1
    this is a simple program i wrote to illustrate here. I have a test setup which uses -fno-exceptions flag and .so file which use throw internally. I cant change the -fno-exception in my test setup, cos it is not controlled by me. And I cannt change the .so either. so the question – Kiran Aug 19 '15 at 11:57

3 Answers3

18

You cannot use -fno-exceptions flag with program, that use exceptions (try/catch/throw).

Quote from gcc manual

Before detailing the library support for -fno-exceptions, first a passing note on the things lost when this flag is used: it will break exceptions trying to pass through code compiled with -fno-exceptions whether or not that code has any try or catch constructs. If you might have some code that throws, you shouldn't use -fno-exceptions. If you have some code that uses try or catch, you shouldn't use -fno-exceptions.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Thanks I didnt knew that. Then where to use -fno-exceptions ?? Just to avoid throwing exception by some other code like div by 0 ?? I just saw #if __cpp_exceptions # define __try try # define __catch(X) catch(X) # define __throw_exception_again throw #else # define __try if (true) # define __catch(X) if (false) # define __throw_exception_again #endif in the docs – Kiran Aug 19 '15 at 12:00
  • @Kiran simple answer is never. But if you need - only with code, that don't throw exceptions. DIV by 0 is no exception. Read documentation about this flag. – ForEveR Aug 19 '15 at 12:14
  • I have a client code which uses exceptions all the time. And I have a test setup where the compilation flag is set to -fno-exception and I cann't control flags there. I need to integrate client lib into my overall project. How to deal with this situation?? – Kiran Aug 19 '15 at 12:30
  • @Kiran Your two options are to change either the client or your test setup. – molbdnilo Aug 19 '15 at 12:42
  • Using `-fno-exceptions` is a way to have useful branch coverage data, see https://stackoverflow.com/a/43726240/1945549. – Roland Sarrazin Sep 26 '19 at 12:34
5

You can't compile a program with this flag if you throw exceptions in your own code. The -fno-exceptions flag does the following:

  1. All exception handling in STL libraries are removed; throws are replaced with abort() calls
  2. Stack unwind data and code is removed. This saves some code space, and may make register allocation marginally easier for the compiler (but I doubt it'll have much performance impact). Notably, however, if an exception is thrown, and the library tries to unwind through -fno-exceptions code, it will abort at that point, as there is no unwind data.

This will, effectively, turn all exceptions into abort()s, as you would like. Note, however, that you will not be allowed to throw - any actual throws or catchs in your code will result in a compile-time error.

Community
  • 1
  • 1
VP.
  • 15,509
  • 17
  • 91
  • 161
5

I found a workaround. Except for the "e" in the catch block the code can be rewritten like

#include <iostream>
using namespace std;
int main () {
  __try
  {
    __throw_exception_again 20;
  }
  __catch (int e)
  {
    cout << "An exception occurred." << '\n';
  }
  return 0;
}

and can be compiled with

g++ throw.cc -o out -fno-exceptions.
Kiran
  • 499
  • 2
  • 6
  • 17
  • 13
    `__try` and `__catch` are macros that convert to `if (true)` and `if (false)`, respectively, if CPP exceptions are disabled. Your try/catch blocks are thus not doing anything in this case. – ZachB May 04 '18 at 21:36