Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all type of assertion

Tags:

c++

I need a condition like:

try
{
//bug condition
}
catch()
{
//Remove file
}

That is I create one very confidential file which data cannot be viewed by third party, but when any bug occurred in code my file is deleted because I don't known exactly, where bugs occurred.

So I want to catch that bug using try and catch and want to delete that file. How can I catch any exception in C++??

That will delete file if there is a bug.

Like:

try
{
    char TempArray[10];
    char c = TempArray[11];
}
catch
{
    cout<<"Array out of boundry";
    deleteFile("Confi.txt");
}
like image 481
Santosh Dhanawade Avatar asked May 13 '13 09:05

Santosh Dhanawade


People also ask

How do you catch an assertion?

In order to catch the assertion error, we need to declare the assertion statement in the try block with the second expression being the message to be displayed and catch the assertion error in the catch block.

How do you fix an assertion error in Python?

If the assertion fails, Python uses ArgumentExpression as the argument for the AssertionError. AssertionError exceptions can be caught and handled like any other exception using the try-except statement, but if not handled, they will terminate the program and produce a traceback.

How do you resolve assertion errors?

In order to handle the assertion error, we need to declare the assertion statement in the try block and catch the assertion error in the catch block.

What is assertion in exception handling?

Assertion is used for debugging of the required assumptions to be checked at runtime only by enabling the assert feature while exception is to check the specific conditions to be checked during execution of the program to prevent the program from terminating.


3 Answers

A word about security: If you create a file, on hard disk, with confidential information, anyone can shut down the computer while the process is running and the file is still open, take out the hard drive and read its contents.

If the file is on the server you can do basically the same thing by pausing your process before it deletes the file.

Even if you removed the file from the filesystem, most likely it can still be read, since removing a file does not wipe its contents.

I'd recommend not to deal with confidential information until you have the needed expertise - not by learning from SO. But if you have to do it, I think the watchdog process suggested here + encryption is the way to go.

like image 130
Elazar Avatar answered Nov 13 '22 14:11

Elazar


FIRST OF ALL:

You don't want to do that.

Exceptions are not meant for handling bugs, but run-time error conditions that make it impossible for your function to satisfy the pre-conditions of other functions it has to call, or to keep the promise of fulfilling its own post-conditions (given that the caller has satisfied the pre-conditions). See, for instance, this article by Herb Sutter.

Don't ever write anything like this:

try
{
    //bug condition <== NO! Exceptions are not meant to handle bugs
}
catch()
{
    //Remove file
}

But rather:

assert( /* bug condition... */ );

BACK TO THE QUESTION:

Your program has undefined behavior, and most likely it will not throw any exception at all when you do:

char TempArray[10];
char c = TempArray[11];

So catching all exceptions won't help. This is a bug, i.e. a programming error, and it is arguable whether you should handle bugs in a way that transfer controls to a buggy routine; moreover, if you are admitting the presence of bugs in your program, couldn't you just be transferring control to a buggy handler? That may make it even worse.

Bugs should be dealt with by preventing them, making use of assertions, perhaps adopting methodologies such as test-driven development, and so on.

This said, regarding a way to catch all exceptions, you can do:

try
{
    // ...
}
catch (...) // <== THIS WILL CATCH ANY EXCEPTION
{
}

But using catch (...) is discouraged as a design guideline, because it easily leads to swallow error conditions that are meant to be handled and forget about them. After all, exceptions were invented exactly to prevent programmers to forget checking error codes, and catch (...) makes that so easy.

For a catch-everything purpose, it would be better to let all of your exceptions derive from std::exception, and then do:

try
{
    // ...
}
catch (std::exception& e)
{
    // Do something with e...
}
like image 40
Andy Prowl Avatar answered Nov 13 '22 13:11

Andy Prowl


What you want to use is RAII. In this case create class which in constructor take name of file and in destructor it delete the file. Before doing anything with the file you instantiate object of such class with appropriate name and later on if for whatever reason the function exits (cleanly or by means of exception), the file is going to be deleted.

Example code:

class FileGuard : public boost::noncopyable {
    std::string filename_;

    public:
    FileGuard(const std::string &filename) : filename_(filename)
    {}

    ~FileGuard()
    {
        ::unlink(filename_);
    }
 }
like image 2
anydot Avatar answered Nov 13 '22 12:11

anydot