sunnysky 0 Report post Posted March 11, 2007 Hi Programmers,To my knowledge, a try block can stand alone. But my compiler keepcomplaining untill I put a catch block after a try block. Below isthe 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.cpptest_try_catch.cpp: In function `int f(int)':test_try_catch.cpp:11: error: expected `catch' before numeric constanttest_try_catch.cpp:11: error: expected `(' before numeric constanttest_try_catch.cpp:11: error: expected identifier before numericconstanttest_try_catch.cpp:11: error: invalid catch parametertest_try_catch.cpp:11: error: expected `)' before numeric constanttest_try_catch.cpp:11: error: expected `{' before numeric constant Share this post Link to post Share on other sites
WeaponX 0 Report post Posted March 11, 2007 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.aspxDoes your program work out as expect when you put catch after the try block? Share this post Link to post Share on other sites
iGuest 3 Report post Posted March 11, 2007 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
pyost 0 Report post Posted March 11, 2007 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
bluefish1405241537 0 Report post Posted March 18, 2007 A try block really does nothing on its own. If you want it to ignore exceptions, just put an empty catch block as shown above. The compiler doesn't do this automatically because there is to reason for it to "think" that this is the intended effect. Share this post Link to post Share on other sites
vizskywalker 0 Report post Posted March 18, 2007 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