User forums > General (but related to Code::Blocks)

Compile error

<< < (4/5) > >>

Michael:

--- Quote from: killerbot on February 07, 2006, 11:13:51 am ---Normally when new fails, it throws -> catch it.
There exist a no_throw new. I think it is not portable, the gurus (Sutter, ..) give the advice not to use it. So just forget you ever heard about it ;-)

--- End quote ---

Normally, I always use the throw new :). But, there was a mention of the no-throw new in the book C++Primer (4th Edition) written by
Stanley B. Lippman et al. I will give a check in the C++ standard book later. Until now, I have just found that you can overload the operator, but how this will influence the portability, it was not written :(.

Michael

sethjackson:
How do I use try/catch?


--- Code: (cpp) ---        unsigned char* data;

        // Try to allocate some memory.

        try
        {
             data = new unsigned char[imageSize];
        }

        // Show an exception message if we can't allocate the memory we need.

        catch(bad_allocation)
        {
            wxSafeShowMessage(_("Exception"), _("Memory allocation failed! The application will terminate immediately..."));
        }


--- End code ---

Sorry I'm new to this type of error handling....  :P

thomas:
You need to provide a type in the catch ( ) clause. Except for that, it's ok.

Alternatively, you can use catch ( ... ) if you want to catch all. Then, however, you don't know what you have caught (often, that's acceptable).

EDIT:
Oh yes... the reason why you have to provide a type is that you can catch different things from the same try block (in case that more than one thing can go wrong). The catch block whose type matches the exception thrown is executed and you get the exception similar to a function argument.

thomas:
Another thing: You are using exceptions in an OpenGL application. I don't know what exactly you're programming there, but if it is something that is really CPU-intensive (which may happen to be the case here), then you should be aware that exceptions are not free.

Catching an exception (which luckily only happens in the exceptional case) takes a lot of CPU time, on the order of tens to hundreds of microseconds.
However, even if you do not throw and catch exceptions, your code runs somewhat slower (something on the order of 2-4%, depending on the compiler and the program). Often, this does not matter much, but then, sometimes it does. Thus, think clearly whether you really need exceptions in a CPU-intensive application.

Game_Ender:
Thomas makes a good point but I don't you think you should throw them away out of hand.  Almost everyone spends time making "optimizations" on areas that don't need any.  I say use exceptions carefully and if you run into performance problems pull back there use.

Take a look at Ogre.  It is a high performance 3D rendering engine.  It uses exceptions, but only where they are appropriate, like when a resource fails to run or you have done something to crash the render system.

The overall important rule for exceptions to use them for program flow control, that means don't check for thing with exceptions.  This rough java snippet below is a good example of what not to do:


--- Code: (java) ---int[] myArray = new int[10];

try {
  int index = 0;
  while(true) {
    system.out.println(myArray[index])
    index++;
  }
} catch (ArrayIndexOutOfBoundsException e)
  // Finished the loop
}


--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version