Solved

what different between them.

Posted on 2003-11-03
4
222 Views
Last Modified: 2010-04-01
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
Comment
Question by:sally1980
4 Comments
 
LVL 4

Accepted Solution

by:
n_fortynine earned 20 total points
ID: 9676239
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9676471
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
 
LVL 9

Expert Comment

by:tinchos
ID: 10242497
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now