?
Solved

Unable to  find option for Exception Handling

Posted on 2013-01-24
7
Medium Priority
?
523 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 35

Accepted Solution

by:
sarabande earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 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