📜  在Java中使用构造函数的用户定义异常

📅  最后修改于: 2022-05-13 01:54:48.288000             🧑  作者: Mango

在Java中使用构造函数的用户定义异常

在Java中,我们已经定义了异常类,例如 ArithmeticException、NullPointerException 等。这些异常已经设置为在预定义的条件下触发,例如当您将数字除以零时,它会触发 ArithmeticException。

在Java中,我们可以创建自己的异常类并使用 throw 关键字抛出该异常。这些异常称为用户定义自定义异常。

问题陈述:实现一个Java类Matrix来表示实数的二维矩阵。该类应导出以下方法:

  • Matrix(int n, int m):创建一个大小为 nxm 的矩阵的构造函数,所有值最初都设置为 0;
  • 矩阵乘积(Matrix m):如果两个矩阵具有兼容的维度,则返回对象与m的乘积矩阵,否则返回null;如果矩阵的维数对于矩阵的乘法是错误的,则在方法 check() 中抛出的ExceptionWrongMatrixDimension

例子:

Java
// Java program to create user defined
// exceptions
  
import java.util.Scanner;
  
// User defined exception class to store the exception
// message
class ExceptionWrongMatrixDimension extends Exception {
  
    public ExceptionWrongMatrixDimension(String str)
    {
        // stores the exception message to be displayed
        super(str);
    }
}
  
class twoDimensionalMatrix {
  
    void Matrix(int n, int m)
        throws ExceptionWrongMatrixDimension
    {
        // initialize matrix to be processed
        int[][] matrix = { { 1, 2 },
                           {
                               4,
                               5,
                           } };
  
        System.out.println("\nMatrix is :");
  
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
  
        int rows = 2;
        int cols = 2;
  
        if (n != rows) {
  
            // throw keyword for an exception in a method
            throw new ExceptionWrongMatrixDimension(
                "Invalid matrix dimensions to multiply");
        }
        else {
            int[][] m_matrix = { { 6, 3 },
                                 {
                                     9,
                                     2,
                                 } };
  
            System.out.println("\nMatrix to multiply is :");
  
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
  
                    System.out.print(m_matrix[i][j] + " ");
                }
                System.out.println();
            }
  
            System.out.println("\nMatrix to multiply is :");
  
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    System.out.print(m_matrix[i][j] + " ");
                }
                System.out.println();
            }
  
            int c[][] = new int[m][n];
  
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < rows; j++) {
                    c[i][j] = 0;
                    for (int k = 0; k < rows; k++) {
                        c[i][j] += matrix[i][j]
                                   * m_matrix[k][j];
                    }
                }
            }
  
            System.out.println(
                "\n\nMatrix after multiplication is");
  
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
  
                    // prints the third matrix containing
                    // the multiplied values
                    System.out.print(c[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}
  
public class Main {
    public static void main(String args[])
    {
        twoDimensionalMatrix matrix
            = new twoDimensionalMatrix();
  
        try {
  
            // block of code to be tested for errors while
            // it is being executed.
            System.out.println("Enter the number of rows");
  
            int n = 2;
  
            System.out.println(
                "Enter the number of columns");
            int m = 2;
            matrix.Matrix(n, m);
        }
  
        catch (ExceptionWrongMatrixDimension e) {
  
            // block of code to be executed, if an error
            // occurs in the try block.
            System.out.println(
                "ExceptionWrongMatrixDimension:");
  
            // returns a method object. The name parameter
            // is passed as a string.
            System.out.println(e.getMessage());
        }
    }
}


输出
Enter the number of rows
Enter the number of columns

Matrix is :
1 2 
4 5 

Matrix to multiply is :
6 3 
9 2 

Matrix to multiply is :
6 3 
9 2 


Matrix after multiplication is
15 10 
60 25