[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to catch all exceptions?

Posted on 2009-02-21
7
Medium Priority
?
1,359 Views
Last Modified: 2013-11-26
I am trying to catch all errors, but Visual Studio in debug mode breaks execution and shows "Unhandled exception" message box. I was trying to manage Visual Studio settings (Debug->Exceptions and Tools->Options->Debuging) but it does not help to solve this.

Please help me to fix this.
#include <QtGui/QApplication>
#include <QMessageBox>
 
 
int main(int argc, char *argv[])
{
	try {
 
		int x = 0;
		int y = 4/x;
		return 1;
	}
	catch (std::exception& e) {
		QMessageBox::critical(NULL, QString("Error"),QString(e.what()));
	}
	catch (...) {
		
		QMessageBox::critical(NULL, QString("Error"),QString("Unknown error!"));
	}
 
}

Open in new window

0
Comment
Question by:doitex
  • 3
  • 3
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 23699429
A division by zero is not a C++ exception. It's a hardware exception. You cannot catch it like that.
0
 
LVL 86

Expert Comment

by:jkr
ID: 23700574
"Unhandled exception" here refers to a Win32 SEH exception, not a C++ Exception, see http://msdn.microsoft.com/en-us/library/ms680657.aspx ("Structured Exception Handling") on these. You can handle these like
#include <QtGui/QApplication>
#include <QMessageBox>
 
 
int main(int argc, char *argv[])
{
        __try { // <--- note the underscores
 
                int x = 0;
                int y = 4/x;
                return 1;
        }
        __except(1) {
 
                QMessageBox::critical(NULL, QString("Error"),QString("SEH Exception"));
        
        }
 
}

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 23700588
Oh, another alternative would be to translate Win32 SEH Exceptions to C++ exceptions using '_set_se_translator()' (http://msdn.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx), e.g.
#include <QtGui/QApplication>
#include <QMessageBox>
 
#include <eh.h>
// compile with: /EHa
 
 
void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    throw std::exception();
}
 
int main(int argc, char *argv[])
{
        _set_se_translator(trans_func);
 
        try {
 
                int x = 0;
                int y = 4/x;
                return 1;
        }
        catch (std::exception& e) {
                QMessageBox::critical(NULL, QString("Error"),QString(e.what()));
        }
        catch (...) {
                
                QMessageBox::critical(NULL, QString("Error"),QString("Unknown error!"));
        }
 
}

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:doitex
ID: 23700600
jkr:

Seems "__try" can't be used with QT4 clases - see compiler error message below:

Cannot use __try in functions that require object unwinding
0
 
LVL 86

Expert Comment

by:jkr
ID: 23700613
Ah, yes, I almost forgot that little catch - what is your exact code (since there are no classes in the above)? You might want to use the translator in this case or wrap your Objects in a function call, e.g.
        __try { 
 
                MyFunctionThatUsesQtClasses();
        }
        __except(1) {
 
                QMessageBox::critical(NULL, QString("Error"),QString("SEH Exception"));
        
        }

Open in new window

0
 

Author Comment

by:doitex
ID: 23701178
tnx jkr

Your solution works! I have found complete source for that:

http://www.thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/3/

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question