Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

what different between them.

Hi! I got 3 question:
1. what different between exit(0) and exit(1)? when are time suitable to use them.
2. what this following mean for and what the different?:
    void fnA();throw();
    void fnA();throw(char*);
3. what is namespace? when we need to use namespace? why we need to use namespace?

Thanks for help!
From: Sally
0
sally1980
Asked:
sally1980
1 Solution
 
n_fortynineCommented:
1. Usually when you terminate a program/process usually it's desirable for other processes to know the minimal reason *why* the program was terminated. So usually exit (0) denotes an "successful" exit, and exit (1) means exit with "error" (there could be many more). These values are conventional, i.e. they've meanings designated to them by the coder.

2. throw( ) does not take any parameter. This is what most standard exception classes would do. throw(char *) would take a char array as argument, usually that would be the exception message, e.g. throw("Cannot allocate memory"). You would have to write appropriate code to handle this if you're writing your own exception handlings.

for example
class myException {
    string errMess;
   public:
    myException(string mess) : errMess(mess) {}
    const string getMess() { return errMess; }
   //...
};

3. Namespaces are usually in large project development. To give an example, say you coded a function void foo(void), and so did Mr. X. Now the compiler will get confused not knowing which one to use when they're put together in the project.
So by doing
namespace myStuff {
   void foo() {...}
}

namespace MrXStuff {
   void foo() {...}
}

You can distinguish them by calling myStuff::foo() or MrXStuff::foo() or do
using myStuff::foo;
foo(); //this is your foo
using MrXStuff::foo;
foo(); //Mr.X foo.
0
 
rstaveleyCommented:
Ref 1:

The exit code from exit(N) is equivalent to the return value from main, when the return value is used.You can test the exit code in a batch file or script.

e.g.
   test.bat
   --------8<--------
   myprog.exe
   if errorlevel 1 goto badexit
   echo That was OK
   goto exit
   :badex
   echo That didn't go well
   :exit
   --------8<--------

Note that the DOS batch file "if errorlevel 1" test tests if the exit code was 1 or above. If you want to test multiple exit codes you need to start by testing the high numbered ones first.

e.g.

  test.sh
  --------8<--------
  #!/bin/sh

   ./myprog
   EXITCODE=$?
   if test $EXITCODE = 1; then
     echo Uh-oh we encountered error 1
   else
      echo The error code was $EXITCODE
   fi
   --------8<--------

Exit code 0 is conventionally used to indicate success and any other number (max 255) indicates an error.

Ref 2:

> void fnA();throw();
> void fnA();throw(char*);

A member function declared with throw() should throw no exceptions. It is some kind of a guarantee. You see it used extensively in the standard library. The semicolon before throw() shouldn't be there in the declaration. Where you see throw(char*), you have a function which may throw a char*.

e.g.

   myprog.cpp:
   --------8<--------
   #include <iostream>
   struct foo {
   public:
       void doitbadly() throw(const char*);
   };

   void foo::doitbadly() throw(const char*)
   {
        throw "Oo-err lummee, missus!";
   }

   int main()
   {
      try {
           foo x;
           x.doitbadly();
      }
      catch (const char *message) {
           std::cerr << "Exception thrown: Message was: " << message << '\n';
           exit(1);
      }
      std::cout << "No exceptions thrown\n";
      return 0;
   }
   --------8<--------
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: n_fortynine {http:#9676239}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now