Solved

Unable to  find option for Exception Handling

Posted on 2013-01-24
7
461 Views
Last Modified: 2014-03-31
In my MDI Project I  was unable to catch errors in try catch block.so I  just tried with
simple dialog based application . I  placed one button on dialog and on its click
I  created divide by zero error. But it does not go in catch block at all. I  searched
and everything in project properties and options  but I  did not able to find option
to turn on exception handling  .Please help its urgent,as project is approaching completion.
I  tried same thing in my home and I  found exception handling doesnot work in home pc also. I  am  using genuine copy of VS 2010 professional on windows 7.0.
MDI project developed in VC++ 2010 (MFC).
          It  appears very silly  but genuinely I  am  not able to locate even by using google.


void CExceptionHandDlg::OnBnClickedButton1()
{
	try
	{
		int j = 0;
		int i = 10/j;
	}
	catch(CException* ex)
	{
		MessageBox(_T("hello"),_T(""),MB_OK);
	}

}

Open in new window


I  even tried all  child classes of CException.
0
Comment
Question by:enthurao
7 Comments
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
CException is an MFC class that has to be created and thrown explicitly.  There is no throwing in your code, so nothing is going to be caught.  See http://msdn.microsoft.com/en-us/library/t078xe4f%28v=vs.80%29.aspx ("Exception Handling in MFC")

Moreover, divide by zero does not throw exceptions in C++ (see http://www.jdl.co.uk/briefings/divByZeroInCpp.html).  Even if it did, you should never rely on exceptions to catch divide by zero, you should instead detect it when it is about to occur:  

// Cannot create abstract class CException
class CMyException : public CException {};

try
{
	int j = 0;
	if ( j == 0 ) throw new CMyException();
	int i = 10/i;
}
catch (CException* pEx)
{
	pEx->Delete();
}

Open in new window

0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
You are confusing C++ exceptions with OS exceptions - divide by zero is an OS exception (a hardware exception) that has a different behavior than C++ exceptions that are entirely software based. OS exceptions have to be handled differently - in Windows you use SEH (structured exception handling) for OS exceptions. See this http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx

There are however way where you can translate an OS exception into a C++ exception so that an OS exception is behind the scenes trapped. There are a few things you can try

Check if in your project settings theres an option with value
Enable C++ Exceptions = Yes with SEH Exceptions (/EHa)

This should let you write code like

try {
}
catch(MyException& m) {
}
catch(...)
{
      std::cout << "OS Exception" << std::endl;
}

Wihtout the /EHa option, the program would just crash.

If you need to be able to converted into a C++ exception then you can use the MS specific _set_set_translator. See the example here

http://msdn.microsoft.com/en-us/library/5z4bw5h5%28v=vs.80%29.aspx
0
 

Author Comment

by:enthurao
Comment Utility
Here divide by zero I  just given as example. Actually  in my project  ,code gets crashed
if  any  error occurs even though I  have written try catch block.I  am  not even able to  handle  crashes due to database  though I  written  catch of CDBException. It  seems that  In  VB it is possible. I  don't  understand how come VC++ does not handle such senarios
0
 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
As I said above, if you are trying to catch an exception that it not being thrown, you will not get your catch block entered.  You need to read the documentation for the functions you are calling that you are think are throwing exceptions, then code your catch block to catch exceptions of either exactly that type or a parent type, if the exceptions are being thrown by pointer/reference.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
the catch (...) also works if SEH exception handling was not switched on for cases where function explicitly were thrown. generally it handles all c++ exceptions which were not handled before by other catch blocks with typed exception arguments.

void throw_exception()
{
    throw std::exception("reason");
}
....
try
{
    throw_exception();
}
catch (...) 
{
      AfxMessageBox(_T("Caught Unknown Exception. Need to exit");
      exit(GetLastError()); 
}

Open in new window


however you would not get further information from that error. even the GetLastError information only would be filled properly if a resource-dependent error has occurred (for example an invalid file access). so you normally don't have enough information to handle the error and go on.

if you want to include exceptions like division-by-zero or access violation you would need to switch on SEH exceptions as ambience has explained. you only should see that recovering from those errors hardly will work cause a division by zero or access to invalid pointers rarely were the first action that went wrong what makes it hard to "repair" and go on. i know only one case where this was achieved to a greater extent, that is when windows calls message handlers by function pointers stored with the window handle. such pointers easily could be invalid because of late messages or early freeing of resources. if you see a "first chance" exception message in the output window of the visual studio, it is well likely to come from that.

Sara
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now