PDA

View Full Version : handle exception in Qt



Qt Coder
4th April 2009, 11:26
hi....
i'm using qt 4.4.0.
I want to know which Qt class to use to raise exception in try...catch block.

Can anyone give me sample code of exception handling.

talk2amulya
4th April 2009, 11:36
Qt doesnt provide exception handling unless u r using Qt concurrent..which is when u use multiple threads

Qt Coder
4th April 2009, 11:50
I want to display last sytsem error I got ..

do we hav anything for this in Qt???


I vc++ I used this function


void GetErrorDescription()
{
// Retrieve the system error message for the last-error code

LPVOID lpMsgBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

LocalFree(lpMsgBuf);
}


but its not usefull in Qt .....

talk2amulya
4th April 2009, 11:59
it wont be useful in Qt..if u wanna use that kind of call, use it in a non-Qt class,i.e not derived from QObject..of course u can call this class in ur Qt class..just keep them seperate..then it'll run on windows..dont forget at the end of the day, Qt uses C++ compiler only

febil
6th April 2009, 06:05
it is not so easy to catch exceptions in QT. mostly the exceptions are occuring due to segmentation faults, floating point exceptions etc....so we need to catch OS signals.

please check whether this is ok for u. Main part of the source added below.

#include <signal.h>
#include <setjmp.h>
#include <exception>

jmp_buf StackBuf;

// Macro for signal sending SIGSEGV/SIGFPE (more signals can be added)
#define SIG_INIT signal( SIGSEGV, TryCatchHandler::SignalHandler ); \
signal( SIGFPE, TryCatchHandler::SignalHandler );
// Macro for Try
#define TRY SIG_INIT try { if(( setjmp( StackBuf )) == 0)
{ // add ur code
// Macro for Catch
#define CATCH } else{ sigrelse( SIGSEGV ); sigrelse( SIGFPE ); \
throw new std::exception(); }} catch(...)

static void SignalHandler( int nSignal_i )
{
longjmp( StackBuf, nSignal_i );
}