0

I'm trying to build a test project in Qt Creator, where I want to catch all exceptions in single catch block even without throw.

For example, the following code works in MS Visual Studio without crash, if I set the option C/C++ -> Code Generation -> Enable C++ Exceptions to /EHa:

int a = 100, b = 0, c = 1;
try
{
    c = a / b;
}
catch (...)
{
    c = 0;
}

But the same code above crashes in Qt Creator (as expected). Is there any QMake flag or option (similar to /EHa option in MS Visual Studio) I can set in Qt .pro file, so that I can avoid crash and catch all exceptions without throw?

Thanks in advance.

Yarram
  • 115
  • 1
  • 11
  • 2
    You are mixing up few things. MS VS is not only editor, `/EHa` is a switch for the compiler (MSVC), not the editor. Qt Creator is an editor, qmake is a build system, they can utilize different compilers. Next thing is definition of an exception. `/EHa` switch wraps system level errors as exceptions. There is no other way to catch all exceptions. `catch(...)` always catches every exception. What `/EHa` does is chaniging the definition of an exception, extending it. This is non-standard, so you need to check for a similar thing specifically for your compiler, which you didn't mention. – luk32 Feb 20 '17 at 09:35
  • @luk32, I agree to your statements. I just want to know any flag/option (similar to **/EHa in MS Visual Studio**) I can set in **Qt .pro** file, which is loaded by **Qt Creator (using GCC compiler) under linux and mac platforms**, which can avoid crash, treat system error as a normal exception, and execute **catch** block immediately. Any help would be much appreciated. Thanks. – Yarram Feb 20 '17 at 11:13
  • @zett42 this is not a duplicate. It is about setting a compiler flag for a specific IDE (QtCreator). The div by zero is merely an example. – Mailerdaimon Feb 20 '17 at 11:46
  • If you're talking about GCC - then it's probably necessary to set signal handlers and do processing yourself. – Velkan Feb 21 '17 at 07:25
  • @Mailerdaimon: Exactly, I'm looking for a GCC compiler flag for IDE Qt Creator, which can treat system errors (e.g. divide-by-zero) as exceptions, to avoid crashes. – Yarram Feb 21 '17 at 10:20
  • @Velkan: Do you mean check values of variables (something like assert) before dividing by zero? – Yarram Feb 21 '17 at 10:22
  • No checking. It uses global handler via `sigaction()` or `signal()`. – Velkan Feb 21 '17 at 12:38
  • @Yarram this is the GCC manual for compiler flags: https://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Code-Gen-Options.html , it looks like -fnon-call-exceptions could work. I post this as an comment as I have no time to test it myself, its just a guess. – Mailerdaimon Feb 21 '17 at 12:56
  • Also read this: http://stackoverflow.com/questions/6614049/adding-extra-compiler-option-in-qt – Mailerdaimon Feb 21 '17 at 12:58
  • @Mailerdaimon: Thanks for the comment, but unfortunately the flag: -fnon-call-exceptions didn't work, the app still crashes. – Yarram Feb 23 '17 at 09:27

0 Answers0