The error in your code did not result in a C++ exception; it was most likely a segmentation fault caused when the QByteArray instance on the stack went out of scope and tried to delete the internal memory you had already deleted in line 6. Try/catch blocks do not trap segmentation faults or other C++ signals (not to be confused with Qt "signals"). In order to trap a C++ segmentation fault signal, you need to install a signal handler for SIGSEGV.How I can protect from application crash with try catch when any exception occur.
However, once your program's memory get corrupted by something like a segmentation fault, it is not a good idea to simply trap it and go on running as if nothing has happened. It is far better to 1) find and fix these errors in the debugger instead of pretending they don't exist, and 2) if you do trap them with a signal handler, then the best thing to do is to tell the user that it happened and exit quickly. It really isn't safe to do anything else, because you don't know what damage the SIGSEGV has caused.
Bookmarks