|
|
|
|
![]() ![]() |
Mar 11 2007, 01:40 AM
Post
#1
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 1 Joined: 4-March 07 Member No.: 20,722 |
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 CODE #include <iostream> using namespace std; int f(int i) { try { cout << ++i << endl; } return 1; } int main() { int n = 1; f(n); return 1; } QUOTE 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 |
|
|
|
Mar 11 2007, 04:39 AM
Post
#2
|
|
|
Way Out Of Control - You need a life :) Group: Members Posts: 1,086 Joined: 21-June 05 From: New York Member No.: 6,440 myCENTs:86.41 |
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: http://msdn2.microsoft.com/en-us/library/6dekhbbc.aspx Does your program work out as expect when you put catch after the try block? |
|
|
|
Mar 11 2007, 05:25 AM
Post
#3
|
|
|
PESTICIDAL MANIAC Group: Members Posts: 626 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
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. CODE 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 |
|
|
|
Mar 11 2007, 09:55 AM
Post
#4
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,049 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 myCENTs:9.92 |
What's more, you could just add a catch block that would be empty.
CODE try { cout << ++i << endl; } catch { } Not quite clean, but works. |
|
|
|
Mar 18 2007, 05:33 AM
Post
#5
|
|
|
Member [ Level 2 ] Group: Members Posts: 71 Joined: 16-December 06 Member No.: 18,419 |
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.
|
|
|
|
Mar 18 2007, 08:21 PM
Post
#6
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
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 |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 5th December 2008 - 01:14 AM |