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.
Example:
// 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());
}
}
}
Output
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