Exception
-
Hi, I want to capture a exception. I used try catch but its not working.Im using the following code. Please suggest me. LPSTR szError; try { int i = 10; int j = 0; int k = i/j; } catch(CException *e) { e->GetErrorMessage(szError,200,NULL); } Thanks,
-
Hi, I want to capture a exception. I used try catch but its not working.Im using the following code. Please suggest me. LPSTR szError; try { int i = 10; int j = 0; int k = i/j; } catch(CException *e) { e->GetErrorMessage(szError,200,NULL); } Thanks,
-
The CException *e has somebody to throw. And this wont do the "/" operator. Use catch(...) what returns GetLastError() Press F1 for help or google it. Greetings from Germany
-
its not going to the catch block,after executing the i/j statment,the application is getting crashed.
-
Hi, I want to capture a exception. I used try catch but its not working.Im using the following code. Please suggest me. LPSTR szError; try { int i = 10; int j = 0; int k = i/j; } catch(CException *e) { e->GetErrorMessage(szError,200,NULL); } Thanks,
There isn't a C++ exception for what you want to catch. You have to use an operating specific method to trap that sort of error. You can convert the operating system error into a C++ exception if the mechanism allows it but it won't happen by default. As an example you can use structured exception handling (SEH) (the OS specific thing that happens on a divide by zero) on windows to throw a C++ exception. Cheers, Ash
-
its not going to the catch block,after executing the i/j statment,the application is getting crashed.
-
-
in this link they have mentioned using exception function we can catch divide by zero error. please chk this link http://support.microsoft.com/kb/815662[^] Thanks,
Make at least sure you don't have any optimizing turned on. It might remove your entire code block. Step through the code in the disassembly window, instruction by instruction, and inspect what happens and make sure all the code you expect is there.
-
There isn't a C++ exception for what you want to catch. You have to use an operating specific method to trap that sort of error. You can convert the operating system error into a C++ exception if the mechanism allows it but it won't happen by default. As an example you can use structured exception handling (SEH) (the OS specific thing that happens on a divide by zero) on windows to throw a C++ exception. Cheers, Ash
As Ash said, the division by zero is not a C++ exception, so you should use platform specific API to deal with it and translate it to a C++ exception. Read this from the MSDN: _set_se_translator (CRT)[^] (it requires to compile with the right flags to make it work properly). Another way to achieve your goal is to use the
__try
/__except
statements (see try-except Statement (C++)[^]). -
in this link they have mentioned using exception function we can catch divide by zero error. please chk this link http://support.microsoft.com/kb/815662[^] Thanks,
That'll work if you're using managed code as that exception comes from the .NET runtime. However as you originally said you want to catch a CException then you're using MFC which (AFAIK, I may be misinformed) doesn't play nicely with .NET and managed code. Cheers, Ash
-
Hi, I want to capture a exception. I used try catch but its not working.Im using the following code. Please suggest me. LPSTR szError; try { int i = 10; int j = 0; int k = i/j; } catch(CException *e) { e->GetErrorMessage(szError,200,NULL); } Thanks,
No exceptions will be thrown in your code.I would recommend your are targeting VC10 and you don't need the best performance possible consider using SafeInt class.It automatically checks for overflows and attempts to divide by zero.
#include <safeint.h> ... using namespace msl::utilities; try{ SafeInt<int> i = 10; SafeInt<int> j = 0; SafeInt<int> k = i/j; } catch(SafeIntException& e) { switch(e.m\_code) { case SafeIntDivideByZero: AfxMessageBox(L"Devide by zero occured!"); break; case SafeIntArithmeticOverflow: AfxMessageBox(L"Overflow error occured!"); break; case SafeIntNoError: AfxMessageBox(L"Other error occured!"); break; } }
Life is a stage and we are all actors!
-
in this link they have mentioned using exception function we can catch divide by zero error. please chk this link http://support.microsoft.com/kb/815662[^] Thanks,
Karthika85 wrote:
we can catch divide by zero error. please chk this link
I did; did you check the note that states: Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. This example is for C++.NET and will only work in managed code, you are trying to use CException[^] which is only for MFC exceptions.
It's time for a new signature.