[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unable to  find option for Exception Handling

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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

834 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