Solved

Unable to  find option for Exception Handling

Posted on 2013-01-24
7
513 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:ADITYA RAO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 38822439
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
ID: 38822646
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:ADITYA RAO
ID: 38831132
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
ID: 38831356
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 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 38854477
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

726 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