Jump to content
xisto Community
sunnysky

Must "try" Have "catch" To Pair?

Recommended Posts

Hi Programmers,

To my knowledge, a try block can stand alone. But my compiler keep
complaining untill I put a catch block after a try block. Below is
the code, what wrong did I do? Thanks.

Brian

#include <iostream>using namespace std;int f(int i){  try {	cout << ++i << endl;  }  return 1;}int main(){  int n = 1;  f(n);  return 1;}

Compiler Message:$ g++ test_try_catch.cpp
test_try_catch.cpp: In function `int f(int)':
test_try_catch.cpp:11: error: expected `catch' before numeric constant
test_try_catch.cpp:11: error: expected `(' before numeric constant
test_try_catch.cpp:11: error: expected identifier before numeric
constant
test_try_catch.cpp:11: error: invalid catch parameter
test_try_catch.cpp:11: error: expected `)' before numeric constant
test_try_catch.cpp:11: error: expected `{' before numeric constant


Share this post


Link to post
Share on other sites

Please include quotes for your code and lines of errors shown. All members here must do that for all programming code they post. I have edited your post to enclose them in quotes.

Now to your question, I'm not a C++ guru or anything, but it looks like the Try, Catch and Throw statement requires this:

https://msdn.microsoft.com/en-us/library/6dekhbbc.aspx

Does your program work out as expect when you put catch after the try block?

Share this post


Link to post
Share on other sites

A Try block can not stand alone, because you're using it to test if an exception is thrown and what to do to handle it. If an exception gets thrown it needs to know what to do with it when it's been caught. If you're not testing for an exception then there's no need to use a try/catch statement.

e.g.

int main() {  try {	throw 13;  }  catch(int exception) {	cout << "An exception occured. Code: " << exception << endl;  }  return 0;}

What this code does is throw an exception with an integer value of 13. The catch statement, caught this exception and does what I tell it to do when an exception occurs, which is to output a string to standard out, and display the exception value thrown.


Cheers,

MC

Share this post


Link to post
Share on other sites

What's more, you could just add a catch block that would be empty.

 

try {	cout << ++i << endl;  }  catch {  }

Not quite clean, but works.

Share this post


Link to post
Share on other sites

Also, C++ was designed to not be a thinking language. It was designed for powerful speedy code. The assumption is that if a programmer wants to do something, they can explicitly have the program do it, so there is no reason for the compiler to do it, but if the programmer doesn't want it done, it is wasteful to do it. So the compiler does not put it an empty catch block moreso because it doesn't assume anything than because there is no reason for it to assume an empty catch block.In regards to the empty catch block, however, it is bad practice to use one. The only time to use a try block is if the program is or could throw an exception. Since this exception will almost definitely impact later code's performance, it should be handled with at least a message to the user explaining the exception. Otherwise, the try block should be left out so the program will fail. Otherwise, unintended and possibly bad results may occur. For example, take a move file function. If it throws an exception when trying to create the new file because it fails, but handles it with a blank catch block, it may then delete the old file anyway. There goes some important data. So if you are using a try statement, always do something, at least output, in the catch.~Viz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.