c++类信息中的 try catch块



这是导致错误的v1

#include <iostream>
#include "exceptionHandlingV2.h"
using namespace std;
int main() {
exceptionHandlingV2 ob(4,0);
cout<<ob.divide();
cout<<"nbye";
return 0;
}
#ifndef EXCEPTION_HANDLING_V2_EXCEPTIONHANDLINGV2_H
#define EXCEPTION_HANDLING_V2_EXCEPTIONHANDLINGV2_H
#include <iostream>
using namespace std;
class exceptionHandlingV2 {
private:
int numerator;
int denominator;
string error;
public:
exceptionHandlingV2();
exceptionHandlingV2(int ,int);
void setDenominator(int );
void setNumerator(int );
void readAgain ();
void print_errors();
double divide() const;
};
#endif //EXCEPTION_HANDLING_V2_EXCEPTIONHANDLINGV2_H
#include "exceptionHandlingV2.h"
exceptionHandlingV2::exceptionHandlingV2(){
numerator=1;
denominator=1;
}
exceptionHandlingV2::exceptionHandlingV2(int numerator,int denominator){
setNumerator(numerator);
try {
setDenominator(denominator);
error = "";
}catch (const char * e){
error = e;
print_errors();
readAgain();
}
}
void exceptionHandlingV2::setDenominator(int denominator){
if ( denominator == 0 ){
throw "divided by zeron";
}
(*this).denominator = denominator;
}
void exceptionHandlingV2::readAgain (){
int temp;
cout<<"enter denominator again other than 0n";
cin >> temp;
exceptionHandlingV2 ( numerator,temp );
}
void exceptionHandlingV2::print_errors(){
cout<<error;
}
void exceptionHandlingV2::setNumerator(int numerator){
(*this).numerator = numerator;
}
double exceptionHandlingV2::divide() const{
return (double)numerator/denominator;
}

这是v2设计我在构造函数

中停止与用户交互
exceptionHandlingV2::exceptionHandlingV2(int numerator,int denominator){
setNumerator(numerator);
setDenominator( denominator );
}
void exceptionHandlingV2::setDenominator(int denominator){
try
{
if ( denominator == 0 )
throw "divided by zeron";
(*this).denominator = denominator;
error = "";
}
catch (const char * e)
{
error = e;
print_errors();
readAgain();
}
}

在v3设计中,我添加了一个新函数来处理类本身内部的异常

exceptionHandlingV4::exceptionHandlingV4(int numerator,int denominator){
setNumerator(numerator);
setDenominator( denominator );
}
void exceptionHandlingV4::setDenominator(int denominator){
try {
runtime_set_denominator_error ( denominator );
}
catch ( const char *e )
{
error = e;
print_errors();
readAgain();
}
}
void exceptionHandlingV4 :: runtime_set_denominator_error  ( int denominator ){
if ( !denominator )
throw "divided by zero n";

(*this).denominator = denominator;
error = "";
}

我做了一个v4设计来停止处理类中的异常,而不是在main()中处理。哪一个是正确的?在类中处理异常或者只是从类中抛出异常并在main中处理?,问题是我不能再继续从用户读取readAgain(),直到用户进入写入输入,因为异常会进入catch块,不能处理catch块中的运行时错误,所以我从设计中删除了readAgain()函数,只在main()部分打印错误

exceptionHandlingV5::exceptionHandlingV5(int numerator,int denominator){
setNumerator(numerator);
setDenominator( denominator );
}
void exceptionHandlingV5::setDenominator(int denominator){
if ( !denominator ){
error = "divided by zero n";
throw "divided by zeron";
}
(*this).denominator = denominator;
error = "";
}
//and also I had to move obj creation 
//out of try block, and therefore 
//because of set_denominator(); handles exception
// I had to forget initializing when creating obj 
//and instead used set methods 
int main() {
exceptionHandlingV5 ob;
ob.setNumerator(4);
try {
ob.setDenominator(0);
// logic
cout << ob.divide();
}
catch(char const *e)
{
// I can only print errors now
// can't keep reading
ob.print_errors();
}
}

感谢@sonulohani, @RichardCritten和@molbdnilo对我的指导和修改我的愚蠢问题,我的意思是,现在我看着我的第一个设计,我发现我犯了很多错误和错误,我得到了你们的帮助和评论。无论如何,我最终修改了我的整个设计,并为除法创建了一个新的类和main()函数。我首先想做的是练习运行时错误处理,现在我想我终于做到了。这是最后的设计。它可能会在未来帮助别人。

#ifndef EXCEPTION_HANDLING_FINAL_V1_EXCEPTION_HANDLING_FINAL_V1_H
#define EXCEPTION_HANDLING_FINAL_V1_EXCEPTION_HANDLING_FINAL_V1_H
class exception_handling_final_v1 {
private:
double numerator;
double denominator;
char * error;
public:
exception_handling_final_v1();
exception_handling_final_v1( double , double );
void set_numerator ( double );
void set_denominator ( double );
double divide ( );
char * errors();
};
#endif //EXCEPTION_HANDLING_FINAL_V1_EXCEPTION_HANDLING_FINAL_V1_H
#include "exception_handling_final_v1.h"
exception_handling_final_v1::exception_handling_final_v1() {
numerator = 1;
denominator = 1;
error = "";
}
exception_handling_final_v1::exception_handling_final_v1( double numerator,
double denominator) {
set_numerator( numerator );
set_denominator ( denominator );
}
void exception_handling_final_v1::set_numerator ( double numerator ){
(*this).numerator = numerator;
}
void exception_handling_final_v1::set_denominator( double denominator ) {
if ( !denominator ){
error = "denominator can not be zeron";
throw "denominator can not be zeron";
}
(*this).denominator = denominator;
error = "";
}
double exception_handling_final_v1 :: divide () {
return denominator / numerator;
}
char * exception_handling_final_v1::errors() {
return error;
}
#include <iostream>
#include "exception_handling_final_v1.h"
using namespace std;
int main() {
exception_handling_final_v1 ob;
double numerator , denominator , result;
cout << "enter nummeratorn";
cin >> numerator;
ob.set_numerator( numerator );
cout << "enter denominatorn";
cin >> denominator ;
try{
ob.set_denominator( denominator );
result = ob.divide();
}
catch( char const * error){
cout << ob.errors();
}
cout<<"bye";
return 0;
}

相关内容

  • 没有找到相关文章

最新更新