Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

what different between them.

Posted on 2003-11-03
4
Medium Priority
?
234 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

719 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