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

exceptions and return statements in c++

本文关键字:返回 语句 异常 c++      更新时间:2023-10-16

我是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 ?
}

您在'throw周围得到了try块。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排除在函数定义之外。

类似的代码

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。使用能够保存某些信息的对象。按值抛出,按引用捕获。