Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: error: exception handling disabled, use -fexceptions to enable

Tags:

c++

c++11

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.

like image 783
Kiran Avatar asked Aug 19 '15 11:08

Kiran


People also ask

How do I enable exception handling?

This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. try − A try block identifies a block of code for which particular exceptions will be activated.

How errors and exceptions are handled in C?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

Which is used to handle the exceptions in C?

Explanation: Exception handler is used to handle the exceptions in c++.

What are the 3 blocks used to handle exception?

The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later. The "finally" block is used to execute the necessary code of the program.


2 Answers

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.

like image 111
ForEveR Avatar answered Oct 10 '22 12:10

ForEveR


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.
like image 29
Kiran Avatar answered Oct 10 '22 12:10

Kiran