[SOLVED] Exceptions, try...catch vs std::set_new_handler. C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Seriously, if you want your application to "abort" at the first sign of allocation-trouble, then who cares. I'd be more worried (concerned) at the application that is so poorly developed that there is ever a concern about whether memory allocation is a concern.
In other words, if your application has exhausted all available memory, what's the next step? Allocate more memory so that you can execute the "special" command to launch a rocket towards the Moon?
Last edited by dwhitney67; 03-22-2013 at 04:08 PM.
In that specific example, where you abort on allocation error, this without exception could be better. Exception means more code, need of enabling it, if not used elsewhere in code, etc. But if you want to do more then just quit your application, then it depends what you want to do.
With "set_new_handler" you can react on allocation errors globally. With exceptions you can react differently on every "new" error, depending on where you write "try" block. Exception also give you opurtunity to skip some code on error (for example this which use allocated memory) and do something else even in another parent function. With "set_new_handler" you can only react on insufficient memory, for example by unallocating some data and then repeat memory allocation or quit. Of course with exception you also can do that, but then need to enclose try-catch in loop. But with exception you can do everything you want if error occur.
Sorry, I do not understand your question, my english is not very good. If you ask about what happen if you use both "set_new_handler" and "try-catch" block, then first is called handler, it can quit application, repeat allocation or throw an exception. If you throw exception then later it can be caught by catch block. By default (when user handler is not set) exception is thrown.
About what to do when you have no free memory, quiting application is a last resort. Depending what you can do, you should free some memory, show message with information what happend and question what to do with user data, save or not, or if your program just executed long task (ex. brute-force cracking password), store current state to resume it later, for example after system reboot or on another machine. If error occured in some program feature you can just end executing of this one module and not whole program. For example: user invoked search feature in editor application on big text file, then it should get information about not possibility to do search and resume normal editing mode.