在功能中捕获并投掷语句

Catch and Throw Statements within A function

本文关键字:语句 功能      更新时间:2023-10-16

我有一个正常运行的matrixtype文件,但是我现在试图将IF语句替换为"尝试,捕获"语句,并在类的函数中丢弃语句。我只是想了解一个功能,以便我可以将其应用于其他功能。由于我确实尝试进行尝试并捕获语句,但它跳过了Try语句,并导致了一个完全阻止程序的实际例外。我关注的一个功能是平等运算符这是headerfile #pragma一次 #包括 #包括 #包括 #包括 #包括 #include

using namespace std;
class matrixType
{
private:
    int **matrix;
    int row;
    int col;
public:
    const matrixType& operator=(const matrixType& mat);
}

这是当前工作的class.cpp文件

#include "matrixType.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdexcept>
#include <limits>
using namespace std;
const matrixType& matrixType::operator=(const matrixType& mat)
{
    if (row != mat.row || col != mat.col)
    {
        cout << "The matrixes are not identical" << endl;
        return *this;
    }
    for (int i = 0; i < row; i++)
    {
        for (int r = 0; r < col; r++)
        {
            matrix[i][r] = mat.matrix[i][r];
        }
    }
    return *this;
}

那是源文件

#include <iostream>
#include <string>
#include <stdexcept>
#include <limits>
#include "matrixType.h"
using namespace std;
int main()
{
    matrixType test1(3, 3);
    matrixType test2(2, 2);
    test1 = test2;
    return 0;
}

所以最大的问题是您如何做例外我尝试了

Try
{
   if(mat.row != row || mat.col != col)
        throw exception("The matrixes are not identical")
}
catch (exception & e)
{
   cout << e.what << endl;
}

导致实际例外弹出,但是当我离开if语句时,它有效,并且不会陷入失败状态。有人看到我在用上面的代码替换if语句的情况吗?

首先,关于异常含义的注释。在此外,我将解决您的代码问题。

在英语中,在C 上抛出例外的含义大致是:"我现在无法处理这种情况,我现在就放弃了,我希望其他人能够处理这个情况。"捕捉的含义大致是:"糟糕,有人搞砸了,但我现在要为此做些事情。"考虑到这一点,让我们看一下它的工作方式更具体的细节。

许多函数具有,这些功能是函数在运行之前始终期望的事物。例如,squareRoot(double x)函数可能具有x绝不是负面的前提条件。当违反函数的先决条件时,这是一个自然的时刻,说"我现在就放弃,处理它!"由于有人称之为该功能的人在做错了什么,而squareRoot无法解决问题。这看起来如下:

// precondition: x must not be negative
double squareRoot(double x){
    if (x < 0.0){
        // precondition is violated
        throw std::runtime_error("input to squareRoot was negative");
        // the function exits here!
        // this point in code is not reachable
    }
    // otherwise, precondition is satisfied, it's safe to continue
    ...
    return result;
}

在其他地方,在野外:

void myFunction(){
    double x;
    cout << "x = ";
    cin >> x;
    double y = squareRoot(x);
    cout << ", y = " << y << 'n';
}

在这里,如果某人键入负数,则违反了squareRoot的先决条件,并且会引发异常。这意味着squareRoot 不会正常返回,而myFunction也不会。当抛出异常时,一切都会中断,直到 捕获异常,这可能会在当前功能之外发生。

int main(){
    try {
        myFunction(); // this might throw
        // if myFunction() throws, the following output will not happen!
        cout << "Thanks! have a nice day.n";
    } catch (std::runtime_error e) {
        // if myFunction() does throw, the error lands right here
        cout << "Oops! an error occurred: " << e.what() << 'n';
    }
    return 0;
}

重要的是要注意,只有当某事真正错误时,您才应抛出异常。例外是不是替代普通程序逻辑的替代品,它们不是替代品用于验证用户输入之类的内容。


关于自定义类:

当您定义自定义类类型时,如果它具有复制构造函数,复制分配运算符或驱动器,则可能意味着您的类具有敏感信息,需要特殊护理才能清理并跟踪。如果您写了至少其中一个功能,则应实现所有功能。否则,在您的程序中引入内存泄漏,错误的数据共享以及各种不必要的行为非常容易。


关于您的副本分配操作员:

复制分配运算符的目的,就像a = b;中一样,在运行后,左手对象应与 没有改变。此时,左手对象的先前状态丢失了。在逻辑上与之完全相同的含义是您的班级代表什么的问题。

在一个简单的矩阵中,我希望两个矩阵在所有元素的宽度,高度和值相同时都相同,并且我将相应地实现一个复制分配运算符。例如:

Matrix& Matrix::operator=(const Matrix& other){
    releaseMemory(); // clean up old data
    resize(other.rows, other.cols); // allocate new data
    for (int i = 0; i < rows; ++i){
        for (int j = 0; j < cols; ++j){
            this->data[i][j] = other.data[i][j]; // copy each value
        }
    }
    return *this;
}

我将详细信息的实现留给您。您应该能够找到有关如何超载操作员,编写自己的复制构造函数和驱动器的精彩示例和详细说明,以及一般通过阅读一本好的C 书籍的良好实践。

实际上,我弄清楚了我必须使用返回语句,因为当我在捕获函数中包含返回语句时,它可以使用。