Link to home
Start Free TrialLog in
Avatar of ADITYA RAO
ADITYA RAOFlag for India

asked on

Unable to find option for Exception Handling

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.
Avatar of mrwad99
mrwad99
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of ambience
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
Avatar of ADITYA RAO

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial