C++ 方法c+内的异常+;
我试图捕获方法中的异常,但我的代码不起作用,因为异常会在调用析构函数之前关闭程序 main.cpp:C++ 方法c+内的异常+;,c++,C++,我试图捕获方法中的异常,但我的代码不起作用,因为异常会在调用析构函数之前关闭程序 main.cpp: #include "register.h" #include "agenda.h" #include "professionalregister.h" #include "personalregister.h" #include <iostream> #include <stdexcept> int main(){ Agenda agenda (2);
#include "register.h"
#include "agenda.h"
#include "professionalregister.h"
#include "personalregister.h"
#include <iostream>
#include <stdexcept>
int main(){
Agenda agenda (2);
Register *profissional_1 = new ProfessionalRegister ("camila", 1, 2, "engenheira", "computacao"),
*profissional_2 = new ProfessionalRegister ("leticia", 3, 4, "engenheira", "metalurgia");
Register *pessoal = new PersonalRegister ("Amanda" , 5, 6, "Rua", 7);
agenda.insercao(profissional_1);
agenda.insercao(pessoal);
agenda.insercao(profissional_2);
std :: cout << agenda.obter_cadastro(0)->get_nome() << std :: endl;
std :: cout << agenda.obter_cadastro(0)->get_idade() << std :: endl;
std :: cout << agenda.obter_cadastro(0)->get_cpf() << std :: endl;
agenda.obter_cadastro(0)->get_atributos_extras();
std :: cout << agenda.obter_cadastro(2)->get_nome() << std :: endl;
std :: cout << agenda.obter_cadastro(2)->get_idade() << std :: endl;
std :: cout << agenda.obter_cadastro(2)->get_cpf() << std :: endl;
agenda.obter_cadastro(2)->get_atributos_extras();
delete profissional_1;
delete profissional_2;
delete pessoal;
}
#包括“register.h”
#包括“议程.h”
#包括“professionalregister.h”
#包括“personalregister.h”
#包括
#包括
int main(){
议程(2);
寄存器*PROFICENATIONAL_1=新的专业寄存器(“camila”,1,2,“engenheira”,“Computaco”),
*Professional_2=新的专业注册(“leticia”,3,4,“engenheira”,“metalurgia”);
Register*pessoal=新的个人注册(“Amanda”,5,6,“Rua”,7);
议程:INSERCO(1);
议程项目:INSERCO(比索阿尔);
议程:欧洲工商管理学院(第二届会议);
std::cout get_nome()您应该开始使用调试器来跟踪代码,并查看执行代码时发生的情况
但是我知道你是如何错误地使用异常的。你不应该强迫一个异常,你捕捉到的异常会打印一个错误。当你的函数得到一个不是为它设计的输入时,你应该抛出一个异常
Register const* GetAppointment(size_t nr) const {
if (nr < appointments.size()) {
return appointments[nr];
} else {
throw std::out_of_range("Appointment number out of range.");
}
}
你不会捕捉到那个异常。那是结构化异常,而不是C++异常。对C++异常进行重构是可行的(以及非平凡的和OS特定的),但高度泄气。请注意:在C++中,你不需要从动态内存中分配变量。对于动态数组,考虑<代码> STD::vector < /代码>。更不用说使用指针来设计和实现。<代码> ObTryDeCasoRoo> /COD>有未定义的行为。ter*
,但如果从at()
引发异常,则不会返回任何内容。这就是分段错误的来源。
Register const* GetAppointment(size_t nr) const {
if (nr < appointments.size()) {
return appointments[nr];
} else {
throw std::out_of_range("Appointment number out of range.");
}
}
#include <string>
#include <sstream>
class Register{
private:
std::string name;
int idade, cpf;
public:
Register(std::string_view const& name, int idade, int cpf) : name(name), idade(idade), cpf(cpf) {}
virtual ~Register() {}
std::string const& GetName() const { return name; }
int GetIdade() const { return idade; }
int GetCpf() const { return cpf; }
virtual std::string GetExtraAttributes() const = 0;
};
class ProfessionalRegister final : public Register {
private:
std::string function;
std::string speciality;
public:
ProfessionalRegister(std::string_view const& name, int idade, int cpf
, std::string_view const& function, std::string_view const& speciality)
: Register(name, idade, cpf)
, function(function), speciality(speciality) {}
std::string GetExtraAttributes() const override {
std::stringstream out;
out << "Function : " << function << ", Department " << speciality;
return out.str();
}
};
class PersonalRegister final : public Register {
private:
std::string street;
int number;
public:
PersonalRegister(std::string_view const& name, int idade, int cpf
, std::string_view const& street, int number)
: Register(name, idade, cpf)
, street(street), number(number) {}
std::string GetExtraAttributes() const override {
std::stringstream out;
out << "Street : " << street << ", number " << number;
return out.str();
}
};
#include <vector>
#include <memory>
#include <iostream>
#include <stdexcept>
class Agenda {
private:
size_t nrOfAppointments{ 0 };
std::vector<std::unique_ptr<Register>> appointments;
public:
Agenda(size_t capacity) : appointments(capacity) {}
void Add(std::unique_ptr<Register>&& reg) {
if (nrOfAppointments < appointments.size()) {
appointments[nrOfAppointments++] = std::move(reg);
} else {
std::cout << "Failed adding appointment: agenda full.\n";
}
}
Register const* GetAppointment(size_t nr) const {
if (nr < appointments.size()) {
return appointments[nr].get();
} else {
throw std::out_of_range("Appointment number out of range.");
}
}
};
int main() {
Agenda agenda(2);
agenda.Add(std::make_unique<ProfessionalRegister>("camila", 1, 2, "engenheira", "computacao"));
agenda.Add(std::make_unique<ProfessionalRegister>("leticia", 3, 4, "engenheira", "metalurgia"));
agenda.Add(std::make_unique<PersonalRegister>("Amanda", 5, 6, "Rua", 7));
std::cout << agenda.GetAppointment(0)->GetName() << '\n';
std::cout << agenda.GetAppointment(0)->GetIdade() << '\n';
std::cout << agenda.GetAppointment(0)->GetCpf() << '\n';
std::cout << agenda.GetAppointment(0)->GetExtraAttributes() << '\n';
std::cout << agenda.GetAppointment(2)->GetName() << '\n'; // throws an exception
std::cout << agenda.GetAppointment(2)->GetIdade() << '\n';
std::cout << agenda.GetAppointment(2)->GetCpf() << '\n';
std::cout << agenda.GetAppointment(2)->GetExtraAttributes() << '\n';
}