954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Custom exception - throw-delay

Hi again,

Is it possible when throwing a custom exception to pause the program instead of just exiting?
I have a throw customexception("error msg") in my main and I want to print the error msg and then press return to exit or something like that.

Thanks

obscured47
Light Poster
28 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

@obscured47 You can do anything once you catch the exception.
Why do you want to throw an exception to pause the execution? There could be better ways to do that.
Can you post small segemt of your code and let us know the behaviour you are expecting ?

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

Sure,

Say I have:

class custom_exception : public std::runtime_error
{
public:
       custom_exception(string msg) { cout << "Error: " << msg << endl; }
};


and then in my main:

if (x>10) throw custom_exception("number bigger than 10");

After this, the program must end (because it cannot continue with the given data)
I want to throw the exception, the user will see the msg, press return and then terminate.

By the way I know that the code above doesn't make a lot of sense but it's just to show what I mean. Hope its clear :)

obscured47
Light Poster
28 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

You need to catch the exception. You need to write a catch block. If you let OS to handle your first chance exception it is going to simply terminate the process because OS does not know which way you want to terminate.
You can try writing the catch block and exit from there.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

>I want to throw the exception, the user will see the msg, press return and then terminate.
Set your own custom termination handler. Look in your reference for the header to get more details.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanks :) solved!

obscured47
Light Poster
28 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You