Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Exception

Exception

Scheduled Pinned Locked Moved C / C++ / MFC
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K KarstenK

    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

    P Offline
    P Offline
    Pryabu
    wrote on last edited by
    #3

    its not going to the catch block,after executing the i/j statment,the application is getting crashed.

    L K 2 Replies Last reply
    0
    • P Pryabu

      its not going to the catch block,after executing the i/j statment,the application is getting crashed.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      I do not think that divide by zero is a catchable exception; try throwing something within your code. Also take a look at this page[^].

      It's time for a new signature.

      P 1 Reply Last reply
      0
      • P Pryabu

        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,

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #5

        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

        S 1 Reply Last reply
        0
        • P Pryabu

          its not going to the catch block,after executing the i/j statment,the application is getting crashed.

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #6

          you also could write such methords or classes which handles such cases and than throws exception. Press F1 for help or google it. Greetings from Germany

          1 Reply Last reply
          0
          • L Lost User

            I do not think that divide by zero is a catchable exception; try throwing something within your code. Also take a look at this page[^].

            It's time for a new signature.

            P Offline
            P Offline
            Pryabu
            wrote on last edited by
            #7

            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,

            N A L 3 Replies Last reply
            0
            • P Pryabu

              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,

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #8

              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.

              home

              1 Reply Last reply
              0
              • A Aescleal

                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

                S Offline
                S Offline
                Sauro Viti
                wrote on last edited by
                #9

                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++)[^]).

                1 Reply Last reply
                0
                • P Pryabu

                  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,

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #10

                  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

                  1 Reply Last reply
                  0
                  • P Pryabu

                    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,

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    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!

                    1 Reply Last reply
                    0
                    • P Pryabu

                      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,

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups