Click here to Skip to main content
16,015,296 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Exception Handling in C++ Pin
Richard MacCutchan23-Jun-10 3:02
mveRichard MacCutchan23-Jun-10 3:02 
AnswerRe: Exception Handling in C++ Pin
Emilio Garavaglia23-Jun-10 4:47
Emilio Garavaglia23-Jun-10 4:47 
AnswerRe: Exception Handling in C++ Pin
Aescleal23-Jun-10 7:10
Aescleal23-Jun-10 7:10 
GeneralRe: Exception Handling in C++ Pin
VeganFanatic25-Jun-10 16:11
VeganFanatic25-Jun-10 16:11 
GeneralRe: Exception Handling in C++ Pin
Aescleal25-Jun-10 20:17
Aescleal25-Jun-10 20:17 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt27-Jun-10 18:13
Stephen Hewitt27-Jun-10 18:13 
GeneralRe: Exception Handling in C++ Pin
Aescleal28-Jun-10 6:09
Aescleal28-Jun-10 6:09 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt28-Jun-10 15:00
Stephen Hewitt28-Jun-10 15:00 
On the issue of a catch (...) in main as follows (I also have objections on other grounds, but we'll address one issue at a time):
#include "stdafx.h"
#include <tchar.h>
#include <iostream>
#include <stdexcept>

using namespace std;

class MyException : public runtime_error
{
public:
	MyException(const char *pMsg) : runtime_error(pMsg) {}
};

void SomeFunction()
{
	throw MyException("SomeFunction()");
}

int _tmain(int argc, _TCHAR* argv[])
{
	try
	{
		SomeFunction(); // Unexpected exception thrown in here (assume it's deeply nested).
	}
	catch (...)
	{
		cerr << "Caught exception..." << endl;
	}

	return 0;
}


All the error information you get from the unexpected event is the following message:
Caught exception...

 
If the catch (...) is removed a crash dump is generated and the stack extracted from it (using WinDBG) looks as follows:
0:000> .ecxr
eax=00000000 ebx=0024f2d4 ecx=00000000 edx=0008dcd8 esi=770a030c edi=011915e3
eip=742dbb47 esp=0024f1e8 ebp=0024f214 iopl=0         nv up ei pl nz na po nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000202
msvcr90!terminate+0x33:
742dbb47 e879100100      call    msvcr90!__SEH_epilog4 (742ecbc5)
 
0:000> kvn 1000
 # ChildEBP RetAddr  Args to Child              
00 0024f214 0119161f 0024f2a4 77009d57 0024f2d4 msvcr90!terminate+0x33 (FPO: [Non-Fpo]) (CONV: cdecl) [f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\hooks.cpp @ 130]
01 0024f21c 77009d57 0024f2d4 b21db5fb 00000000 Console!__CxxUnhandledExceptionFilter+0x3c (FPO: [Non-Fpo]) (CONV: stdcall) [f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\unhandld.cpp @ 72]
02 0024f2a4 77b10727 0024f2d4 77b10604 00000000 kernel32!UnhandledExceptionFilter+0x127 (FPO: [SEH])
03 0024f2ac 77b10604 00000000 0024f894 77acc3d0 ntdll!__RtlUserThreadStart+0x62 (FPO: [SEH])
04 0024f2c0 77b104a9 00000000 00000000 00000000 ntdll!_EH4_CallFilterFunc+0x12 (FPO: [Uses EBP] [0,0,4])
05 0024f2e8 77af87b9 fffffffe 0024f884 0024f424 ntdll!_except_handler4+0x8e (FPO: [4,5,4])
06 0024f30c 77af878b 0024f3d4 0024f884 0024f424 ntdll!ExecuteHandler2+0x26
07 0024f3bc 77ab010f 0024f3d4 0024f424 0024f3d4 ntdll!ExecuteHandler+0x24
08 0024f3bc 772bb727 0024f3d4 0024f424 0024f3d4 ntdll!KiUserExceptionDispatcher+0xf (FPO: [2,0,0]) (CONTEXT @ 0024f424)
09 0024f75c 742ddbf9 e06d7363 00000001 00000003 KERNELBASE!RaiseException+0x58 (FPO: [4,20,0])
0a 0024f794 0119106a 0024f7b4 01192460 b21db00a msvcr90!_CxxThrowException+0x48 (FPO: [Non-Fpo]) (CONV: stdcall) [f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\throw.cpp @ 161]
0b 0024f804 011912ca 00000001 005920e8 00591e00 Console!SomeFunction+0x65 (CONV: cdecl) [c:\users\stephen.hewitt\documents\visual studio 2008\projects\scratch\console\console.cpp @ 19]
0c 0024f848 76fe3677 7efde000 0024f894 77ad9d72 Console!__tmainCRTStartup+0x10f (FPO: [Non-Fpo]) (CONV: cdecl) [f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c @ 579]
0d 0024f854 77ad9d72 7efde000 72bf8284 00000000 kernel32!BaseThreadInitThunk+0xe (FPO: [1,0,0])
0e 0024f894 77ad9d45 01191412 7efde000 00000000 ntdll!__RtlUserThreadStart+0x70 (FPO: [SEH])
0f 0024f8ac 00000000 01191412 7efde000 00000000 ntdll!_RtlUserThreadStart+0x1b (FPO: [2,2,0]))


This shows what went wrong and where. What's more, this information is sent to Microsoft and can be analysed by the software publisher using Microsoft's Windows Error Reporting[^] website. You get real crash data from real users as well as statistical information on the most common problems.
 
In short, a pretty message saying "oops" isn't as helpful — crashing is a feature.
 
Steve

GeneralRe: Exception Handling in C++ Pin
theCPkid29-Jun-10 3:20
theCPkid29-Jun-10 3:20 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt1-Jul-10 14:31
Stephen Hewitt1-Jul-10 14:31 
GeneralRe: Exception Handling in C++ Pin
theCPkid1-Jul-10 17:45
theCPkid1-Jul-10 17:45 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt1-Jul-10 17:59
Stephen Hewitt1-Jul-10 17:59 
GeneralRe: Exception Handling in C++ Pin
theCPkid1-Jul-10 19:03
theCPkid1-Jul-10 19:03 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt1-Jul-10 19:38
Stephen Hewitt1-Jul-10 19:38 
GeneralRe: Exception Handling in C++ Pin
Aescleal29-Jun-10 7:01
Aescleal29-Jun-10 7:01 
GeneralRe: Exception Handling in C++ Pin
Stephen Hewitt1-Jul-10 14:17
Stephen Hewitt1-Jul-10 14:17 
GeneralRe: Exception Handling in C++ Pin
theCPkid1-Jul-10 19:06
theCPkid1-Jul-10 19:06 
AnswerRe: Exception Handling in C++ [modified] Pin
theCPkid29-Jun-10 3:11
theCPkid29-Jun-10 3:11 
QuestionCRecordset old data problem Pin
eyalle23-Jun-10 0:29
eyalle23-Jun-10 0:29 
AnswerRe: CRecordset old data problem Pin
loyal ginger23-Jun-10 1:52
loyal ginger23-Jun-10 1:52 
GeneralRe: CRecordset old data problem Pin
eyalle23-Jun-10 2:14
eyalle23-Jun-10 2:14 
QuestionRe: CRecordset old data problem Pin
David Crow23-Jun-10 3:48
David Crow23-Jun-10 3:48 
QuestionPure virtual destructor ? Pin
krishna_CP23-Jun-10 0:17
krishna_CP23-Jun-10 0:17 
AnswerRe: Pure virtual destructor ? Pin
CPallini23-Jun-10 0:40
mveCPallini23-Jun-10 0:40 
AnswerRe: Pure virtual destructor ? Pin
Chris Losinger23-Jun-10 1:30
professionalChris Losinger23-Jun-10 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.