User Defined Exceptions using Constructors in Java
In Java, we have already defined, exception classes such as ArithmeticException, NullPointerException etc. These exceptions are already set to trigger on pre-defined conditions such as when you divide a number by zero it triggers ArithmeticException.
In Java, we can create our own exception class and throw that exception using throw keyword. These exceptions are known as user-defined or custom exceptions.
Problem statement: Realize a Java class Matrix to represent bi-dimensional matrices of real numbers. The class should export the following methods:
- Matrix(int n, int m): Constructor that creates a matrix of size nxm, with all values initially set to 0;
- Matrix product(Matrix m): It returns the matrix that is the product of the object and of m, if the two matrices have compatible dimensions, and null otherwise; ExceptionWrongMatrixDimension that is thrown in the method check() if the dimension of the matrix is wrong for the multiplication of the matrix.
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