Solved

what different between them.

Posted on 2003-11-03
4
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 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