c++中的异常和返回语句
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
。使用能够保存某些信息的对象。按值抛出,按引用捕获。
相关文章:
- 返回语句后的代码,没有警告
- 三元运算符在返回语句中给出意外的结果
- 返回非 void 的函数中没有返回语句警告
- 如何使用单个返回语句返回对变量的引用
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 获取上次执行的返回语句的行
- 返回语句中矢量初始化时出现编译错误
- 快速搜索链表未编译,需要返回语句
- 静态成员函数赋值而不带返回语句
- 在返回语句中构造对象时,std::move() 是否有助于或阻止 RVO?
- 为什么返回语句和打印语句返回不同的答案?
- C++ 非 void 函数的崩溃没有返回语句
- 在返回语句中嵌套条件运算符
- 为什么结构化绑定禁用RVO和移动返回语句
- C - 程序返回值而无需返回语句
- 返回语句中的构造函数语法
- 编译器遇到返回语句时会做什么
- 是一个单一的返回语句清洁器
- C NRVO/复制Elision在括号中带有返回语句
- C 编译器 - 被遗忘的返回语句