SolvedReturn statement within a catch

Posts 1–4 of 4 · Page 1 of 1
Return statement within a catch
Why do I get this error message:

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Runtime Error!

Program: C:\*****.exe



This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.


---------------------------
OK
---------------------------

When I try to do this:

Code:
void main()
{
	try
	{
		throw;
	}
	catch (...)
	{
		MessageBox(NULL, "Failed to initialize the programme.", "Error", MB_OK | MB_ICONERROR);
		return;
	}
}
EDIT: Found out what is the problem. You actually need to throw "something" as throw statement is like a function and it need to take 1 argument. So just replace throw; with throw 0; or throw 1; Doesn't matter.
Why are your returning inside the catch? There is not need to do it. (Catch should be used just to save an err log with the exception and/or do something with the exception, etc, pretty much take care of the error/exception. nothing else, the program continues after it anyways, which is the point of it)

Also you need to obviously throw an exception.. thats the point of it.. Why wouldn't you throw something and calling throw?
I use try and catch to terminate the program and halt the incoming lines if an error occurred. That's why I place a return in the catch.
As you can see I don't care what I throw, it is only there to make a jump to the catch. Since the compiler didn't give me an error I thought it is possible to write it like that.
Like if it passes argument 0 by default or something like that. But it turned out it wasn't like so.

Is there an another method for this?
Quote Originally Posted by Jabberwock View Post
I use try and catch to terminate the program and halt the incoming lines if an error occurred. That's why I place a return in the catch.
As you can see I don't care what I throw, it is only there to make a jump to the catch. Since the compiler didn't give me an error I thought it is possible to write it like that.
Like if it passes argument 0 by default or something like that. But it turned out it wasn't like so.

Is there an another method for this?
In the case of actually preventing the following code from executing kinda makes sense then, since finally { } is not supported.

But still, you could just have all your code on the try catch block, and throw when there is a problem. And then return after the catch.

You are lucky because you are using void main() and not the proper one int main(), as it would require you to return something always (returning inside the catch only happens when there was an error)

try
{...}
catch(...)
{...}
return 0 (in case of int)

But nothing special here, just pointing out something not that important.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us