[LC++]exception handling

choube mehul choube_mehul at yahoo.com
Thu Dec 9 19:29:01 UTC 2004


Hello all;

please look at the following code:

Code 1:
void div (int n1, int n2)
{
  if (n2 == 0) {
    throw "divide by zero";
  }

  cout << n1/n2 << endl;
}

int main (int argc, char argv)
{
  int n1 = 10, n2 = 0;


  try {
    div (n1, n2);
  } catch (string msg) {
    cout << msg << endl;
  }

  return 0;
}

Code 2:
void div (int n1, int n2)
{
  try {
    if (n2 == 0) {
      throw "divide by zero";
    }

    cout << n1/n2 << endl;
  } catch (string msg) {
    cout << msg << endl;
  }
}

int main (int argc, char argv)
{
  int n1 = 10, n2 = 0;

  div (n1, n2);

  return 0;
}

which one from the above is correct
Code 1 or Code 2?

which is one is the general practice/efficient?

what are the advantages, disadvantages?

thanks in advance.

mehul.


________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony




More information about the tuxCPProgramming mailing list