c ++中的异常和返回语句

时间:2014-04-20 16:14:54

标签: c++ exception

我是c ++编程的新手,我正在尝试理解c ++中的异常。我做了一个简单的模型情境,展示了我不理解的东西(我希望,我不会把代码弄得太多)。我用几个方法制作了2个基本类(类CPerson基本上是链表)。我的答案是如何通过例外来停止当前任务。我能够调用异常,但任务继续并使程序中的一些混乱。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

class CPerson{
 public:
   CPerson(){
     p_next_person = NULL;
   }
   CPerson* p_next_person; // pointer to next person in linked list
   int Postcode(); // returns postcode of person
   friend ostream& operator<<(ostream& stream, const CPerson& pers){
      cout << pers.ID << pers.postcode;
      return stream;
   }
   char* ID;
   int   postcode;
};
//---------------------------------------------------------------
class CPeople{
 public:
   CPeople(){
     first_person = NULL;
   }
   CPerson Person( const char*  personID); // finds person by ID and returns it
   bool NewPerson( const char*  personID, int person_postcode); // add new person
   CPerson* first_person ; // start of linked list

};

//-----------------------------------------------------------------
int CPerson::Postcode(){
    return postcode;
}
//-----------------------------------------------------------------
CPerson CPeople::Person( const char*  personID){
  CPerson* now;
  now = first_person;
  while(now != NULL){
    if(strcmp(now->ID,personID)==0){
      break;
    }
      now = now->p_next_person;
  }
// our person is in now (or now is NULL - if person wasn't found).
    try{
      if(now == NULL ){
        throw 0;
        // I need to stop code here
      }else return *now;
    }
    catch (int e)
    {
      cout << "bla bla " << e << '\n';
    }
  }
//----------------------------------------------------------

int main(){
 CPeople people;
 int i = 0;
 people.NewPerson( "JAck", 100 );
 people.NewPerson( "Josh", 100 );
// Bob is not in people right now.
  i = people.Person("BOB").Postcode();
  cout << i;
 // gives exception, which is nice. but it also changes i to some nonsence .. how do I fix it ?
  cout << people.Person ( "BOB" );
// gives exception, which is nice. but also gives segmentation fault. how do I fix it ?
}

2 个答案:

答案 0 :(得分:1)

try周围有'throw块。 try块应该在您调用函数的位置,并且应该使用catch来捕获。因此,您的功能将更改为:

CPerson CPeople::Person( const char*  personID){
  CPerson* now;
  now = first_person;
  while(now != NULL){
    if(strcmp(now->ID,personID)==0){
      break;
    }
    now = now->p_next_person;
  }
  // our person is in now (or now is NULL - if person wasn't found).

  if (now == NULL ){
    throw 0;
    // I need to stop code here
    }
    else return *now;
  }

main将如下所示:

int main(){
  try {
    CPeople people;
   int i = 0;
   people.NewPerson( "JAck", 100 );
   people.NewPerson( "Josh", 100 );
   // Bob is not in people right now.
   i = people.Person("BOB").Postcode();
   cout << i;
   // gives exception, which is nice. but it also changes i to some nonsence .. how do I fix it ?
   cout << people.Person ( "BOB" );
   // gives exception, which is nice. but also gives segmentation fault. how do I fix it ?
   }
   catch (int e)
   {
      cout << "bla bla " << e << '\n';
   }
}

请注意,遇到catch后,将执行catch之后的以下语句。这就是为什么你应该从函数定义中获得catch

答案 1 :(得分:0)

像这样的代码

try{
  if( now == NULL ){
    throw 0;
       // I need to stop code here
  } else return *now;
} catch (int e) {
  cout << "bla bla " << e << '\n';
}

完全错过了这一点。耸耸肩(“bla bla”)继续执行,好像什么都没发生一样是不可能的。您要么确保捕获中的所有意外事件都得到满足,要么您应该在更高级别捕获异常。这里:没有函数返回值的定义,这会导致您调用CPeople::Person的麻烦。

您可以使用try - catch包围这些来电;在函数中省略它们然后抛出。

不要抛出0。使用能够保存某些信息的对象。按价值投掷,以参考方式捕获。