Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » No Exceptions cuaght C++(C++ Fails to catch exceptions)
No Exceptions cuaght C++ [message #1794458] Thu, 30 August 2018 20:32 Go to next message
Julius Caesar is currently offline Julius CaesarFriend
Messages: 7
Registered: March 2018
Junior Member
Hey,
i'm facing a wired. None of my exceptions are caught in C++
try
{
//do something
throw 0;
}
catch(const exception& ex)
{
//do some error handling
}


instead of running into that catch part. I'm always stuck in the termination handler. Now i now it's very hard to actually implement exception handling in the compiler because of the stack unwinding.
I've tried 2 eclipse based IDEs. Both of them use gcc. Unvortuanately my favourite IDE fails to catch exceptions.

Now my first question: Are exceptions only part of the compiler/linker settings or is there any other part in the toolchain which is responsible for exception handling.

This is my compiler output (i removed the includes for better reading)
make -j4 all 
Building file: ../Src/User/display/display_section_pwm.cpp
Invoking: MCU G++ Compiler
xxx\Debug
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -std=c++1y '-D__weak=__attribute__((weak))' '-D__packed=__attribute__((__packed__))' -DUSE_HAL_DRIVER -DSTM32F429xx {SOME INCLUDES}  -O2 -g3 -pedantic -Wall -Wextra -Wconversion -fexceptions -ffunction-sections -c -fno-rtti -MMD -MP -MF"Src/User/display/display_section_pwm.d" -MT"Src/User/display/display_section_pwm.o" -o "Src/User/display/display_section_pwm.o" "../Src/User/display/display_section_pwm.cpp"
Finished building: ../Src/User/display/display_section_pwm.cpp



Building target: xxxx.elf
Invoking: MCU G++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -L"C:\Users\Julian\Giessomat\uC\Giessomat\Middlewares\ST\STemWin\Lib"  -fexceptions -specs=nosys.specs -specs=nano.specs -u_printf_float -T"../STM32F429ZITx_FLASH.ld" -Wl,-Map=output.map -Wl,--gc-sections -fno-rtti -o "Giessomat.elf" @"objects.list"  -lSTemWin532_CM4_OS_GCC -lm
Finished building target: Giessomat.elf



As you can see from the oupput: exception handling is enabled in the compiler and the linker.
Do you have any further suggestions on where to look?
Re: No Exceptions cuaght C++ [message #1794460 is a reply to message #1794458] Thu, 30 August 2018 21:11 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
Your code sample doesn't include a catch block for the type being thrown. The throw 0 is throwing an int. Your catch block it catching a exception. There is no conversion from an int to an exception, so the catch block won't be invoked. You need to either change the type that is being thrown to be a subclass of exception or change the catch block to catch an int.

Note that exception handling is added by the compiler. Eclipse isn't involved with catching exceptions.
Re: No Exceptions cuaght C++ [message #1794461 is a reply to message #1794458] Thu, 30 August 2018 21:17 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Don't have much time to experiment but:

o) Check the specs files to make sure exceptions aren't being turned off.
o) bit of a reach buy try temporarily removing the -fno-rtti

the last because:
https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect-Options
Quote:

-fno-rtti
Disable generation of information about every class with virtual functions for use by the C++ run-time type identification features (dynamic_cast and typeid). If you don't use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but G++ generates it as needed

Maybe the "but" part of the documentation somehow doesn't apply.

Update: what David Wegener said, too. Likely the real reason.

[Updated on: Thu, 30 August 2018 21:26]

Report message to a moderator

Re: No Exceptions cuaght C++ [message #1794524 is a reply to message #1794460] Fri, 31 August 2018 20:26 Go to previous messageGo to next message
Julius Caesar is currently offline Julius CaesarFriend
Messages: 7
Registered: March 2018
Junior Member
@david:
yes you are completely right my code sample was wrong
here is a right one
try
{
    throw std::bad_alloc();
} catch (const exception& ex)
{
		bad_alloc_exception_count++;
}


All Exceptions are either std exceptions or exceptions which are derived from std:runtime_error (which itself is derived form std:exception)
Therefore this catch block should catch the exception above


@(other ;))david
What do you mean by
Quote:
Check the specs files to make sure exceptions aren't being turned off.

Which specs files do you mean?

I tried removing the -fno-rtti but i'm still running into the termination handler

make -j4 all 
Building file: ../Src/User/debug/CCustom_Exception.cpp
Invoking: MCU G++ Compiler
xxxx\Debug
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -std=c++1y '-D__weak=__attribute__((weak))' '-D__packed=__attribute__((__packed__))' -DUSE_HAL_DRIVER -DSTM32F429xx  -O2 -g3 -pedantic -Wall -Wextra -Wconversion -fexceptions -ffunction-sections -c -MMD -MP -MF"Src/User/debug/CCustom_Exception.d" -MT"Src/User/debug/CCustom_Exception.o" -o "Src/User/debug/CCustom_Exception.o" "../Src/User/debug/CCustom_Exception.cpp"
Finished building: ../Src/User/debug/CCustom_Exception.cpp


Building target: Giessomat.elf
Invoking: MCU G++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -L"C:\Users\Julian\Giessomat\uC\Giessomat\Middlewares\ST\STemWin\Lib"  -fexceptions -specs=nosys.specs -specs=nano.specs -u_printf_float -T"../STM32F429ZITx_FLASH.ld" -Wl,-Map=output.map -Wl,--gc-sections -o "Giessomat.elf" @"objects.list"  -lSTemWin532_CM4_OS_GCC -lm
Finished building target: Giessomat.elf


Any other ideas?

Thx for your help so far :)

[Updated on: Fri, 31 August 2018 20:29]

Report message to a moderator

Re: No Exceptions cuaght C++ [message #1794532 is a reply to message #1794524] Sat, 01 September 2018 02:16 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Quote:
Which specs files do you mean?

The ones referenced in the linker command
... -specs=nosys.specs -specs=nano.spec ...
Note that there can be a standard specs file, too.

for -spec info
https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options
for spec file syntax
https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html#Spec-Files

I tried the example at cplusplus.com with g++ and it worked.
http://www.cplusplus.com/reference/exception/exception/

The reason for examining the specs files is they may contain options that override -fexceptions.
Instead of reading them, try moving -fexceptions to the end of the compile and link commands.

[Updated on: Sat, 01 September 2018 02:40]

Report message to a moderator

Re: No Exceptions cuaght C++ [message #1794544 is a reply to message #1794532] Sat, 01 September 2018 21:24 Go to previous message
Julius Caesar is currently offline Julius CaesarFriend
Messages: 7
Registered: March 2018
Junior Member
Hey,
that was indeed a very good hint
I simply removed the nanospecs and it worked.
seems like the namo specs file tries to keep the included libraries very very small.
Thank you both for your help :)
Previous Topic:Can't find error location "Line 0" :-(
Next Topic:"gdb": Launching failed debugging in docker
Goto Forum:
  


Current Time: Thu Mar 28 11:35:22 GMT 2024

Powered by FUDForum. Page generated in 0.02802 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top