Astahost.com   Mar 14, 2010
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Must "try" Have "catch" To Pair?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Must "try" Have "catch" To Pair?

sunnysky
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

 

 

 


Comment/Reply (w/o sign-up)

WeaponX
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?

Comment/Reply (w/o sign-up)

mastercomputers
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

Comment/Reply (w/o sign-up)

pyost
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.

Comment/Reply (w/o sign-up)

bluefish
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.

Comment/Reply (w/o sign-up)

vizskywalker
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

 

 

 


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics
Looking for , catch, pair,



See Also,

*SIMILAR VIDEOS*
Searching Video's for , catch, pair,
advertisement




Must "try" Have "catch" To Pair?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com



Creative Commons License