我有一个Fraction类程序。我决定添加异常块try-catch。但是当我写道:
Fraction h;
try
{
cin >> h;
}
catch(invalid_argument& e)
{
cout << e.what() << endl;
}
它不起作用。程序崩溃了。 但如果我改变这行代码:
try
{
Fraction h(1, 0);
}
catch(invalid_argument& e)
{
cout << e.what() << endl;
}
效果很好。
请帮助我理解为什么它以这种方式工作。感谢。
这是我的代码:
Fraction.h
#pragma once
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int GreatestCommonDivisor(int m, int n); //division method to find the
greatest common divisor
class Fraction; //
ostream& operator<<(ostream& out, const Fraction &fra); //
istream& operator>>(istream& in, Fraction& fra); //The three act meet the
logic compilation and declare the VC6.0
class Fraction {
private:
int fenzi, fenmu; //Molecular, the denominator
public:
Fraction() {}
~Fraction() {}
Fraction(int fenzi, int fenmu);
Fraction operator+(const Fraction &fra)const;
Fraction operator-(const Fraction &fra)const;
Fraction operator*(const Fraction &fra)const;
Fraction operator/(const Fraction &fra)const;
friend ostream& operator<<(ostream& out, const Fraction& fra);//i/O must use a heavy overload
friend istream& operator>>(istream& in, const Fraction& fra);
void setValue(int mu, int zi);
void error_print()
{
cout << "The denominator can not be 0!" << endl;
}
};
Fraction.cpp
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>
#include "Fraction.h"
using namespace std;
int GreatestCommonDivisor(int m, int n)
{
int r;
if (n == 0) throw invalid_argument("Error");
else
{
do
{
r = m % n;
m = n;
n = r;
} while (r != 0);
}
//}
return m;
}
Fraction::Fraction(int zi, int mu) :fenzi(zi), fenmu(mu) {
if (mu == 0)
{
throw invalid_argument("Error");
}
/*try
{
if (mu == 0) {
//cout << "The denominator can not be 0!" << endl;
//exit(0);
}
}
catch(...)
{
cout << "Bad";
}*/
}
ostream& operator<<(ostream& out, const Fraction &fra) {
if (fra.fenmu<0)
out << "(-" << fra.fenzi << "/" << -fra.fenmu << ")"; //Negative
scores
else if (fra.fenzi<0)
out << "(-" << -fra.fenzi << "/" << fra.fenmu << ")";
else if (fra.fenmu == 1)
out << fra.fenzi; //Integer show
else
out << fra.fenzi << "/" << fra.fenmu; //Positive
scores
return out;
}
istream& operator>>(istream& in, Fraction& fra) {
int mu, zi;
cout << "Please enter the denominator and numerator" << endl;
in >> mu >> zi;
fra.setValue(mu, zi);
return in;
}
void Fraction::setValue(int mu, int zi) {
fenmu = mu;
fenzi = zi;
}
Fraction Fraction::operator+(const Fraction &fra)const {
int a = fenzi, b = fenmu;
a = fra.fenmu*fenzi + fra.fenzi*fenmu;
b *= fra.fenmu;
int g = GreatestCommonDivisor(a, b);
if (g != 1)
{
a /= g;
b /= g;
}
return Fraction(a, b);
}
Fraction Fraction::operator-(const Fraction &fra)const {
int a = fenzi, b = fenmu;
a = fra.fenmu*fenzi - fra.fenzi*fenmu;
b *= fra.fenmu;
int g = GreatestCommonDivisor(a, b);
if (g != 1)
{
a /= g;
b /= g;
}
return Fraction(a, b);
}
Fraction Fraction::operator*(const Fraction &fra)const {
int a = fenzi, b = fenmu;
a *= fra.fenzi;
b *= fra.fenmu;
int g = GreatestCommonDivisor(a, b);
if (g != 1)
{
a /= g;
b /= g;
}
return Fraction(a, b);
}
Fraction Fraction::operator/(const Fraction &fra)const {
return *this*Fraction(fra.fenmu, fra.fenzi);
}
div.cpp
// Drob.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include "Fraction.h"
int main()
{
/*Fraction a(-1, 24);
Fraction b(1, 4);
Fraction c = a + b;
Fraction d = a - b;
Fraction e = b - a;
Fraction f = a*b;
Fraction g = a / b;
cout << "a= " << a << endl
<< "b= " << b << endl
<< "a+b=" << c << endl
<< "a-b=" << d << endl
<< "b-a=" << e << endl
<< "a*b=" << f << endl
<< "a/b=" << g << endl;*/
Fraction h;
Fraction i;
try
{
//Fraction a(1, 0);
cin >> h;
cout << h << endl;
cin >> i;
cout << i << endl;
}
catch(invalid_argument& e)
{
cout << "Erroro" << endl;
cout << e.what();
}
Fraction c = h + i;
Fraction d = h - i;
Fraction e = h - i;
Fraction f = h * i;
Fraction g = h / i;
cout << "a= " << h << endl
<< "b= " << i << endl
<< "a+b=" << c << endl
<< "a-b=" << d << endl
<< "b-a=" << e << endl
<< "a*b=" << f << endl
<< "a/b=" << g << endl;
system("pause");
return 0;
}