簡體   English   中英

C ++中的異常和return語句

[英]exceptions and return statements in c++

我是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 使用能夠保存某些信息的對象。 按價值投擲,按參考捕獲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM