Joe
Joe

Reputation: 2633

If I throw a new exception in a constructor do I have to catch this exception whenever I use this constructor?

Sorry if I didnt ask it properly, I'm taking a java class in university now and i'm a beginner..

So what I want to ask is, I created a constructor and declared that this constructor throws an exception. Now when I created a method for this class called add I got an error that I didnt catch the error for the new (Matrix) object I created in this method.

So I wanted to ask if whenever I will use this constructor anywhere I have to catch it? can I just catch it in the constructor and it will not bother me again or that might bring trouble..?

This is my code:

 public class Matrix implements Arithmetic, InputOutput {

        private static final Matrix Matrix = null;
        // Class attributes
        int [][] data;
        private Scanner myScanner;


        // Class constructor - can be created only when a user provide positive columns and rows
        public Matrix(int r, int c) throws DidNotProvideMatrixData
        {
            if (r <= 0 || c <= 0) {
                throw new DidNotProvideMatrixData("There got to be Rows and Columns and they have to be positive!");
            } else {
                this.data = new int[r][c];
            }
        }


        public Matrix add(Object o) 
        {
            try {

                if (o instanceof Matrix && o != null) {

                    Matrix matrixToAdd = (Matrix)o;

                    if (this.data.length == matrixToAdd.data.length && this.data[0].length == matrixToAdd.data[0].length) {

                        Matrix resultMatrix = new Matrix(this.data.length, this.data[0].length);

                        for (int i = 0; i < this.data.length; i++) {
                            for (int j = 0; j < this.data[i].length; j++) {
                                resultMatrix.data[i][j] = this.data[i][j] + matrixToAdd.data[i][j];
                            }
                        }
                        return resultMatrix;
                    } else {
                        throw new AddedDiffSizedMatrix();
                    }
                }
            }

            catch (AddedDiffSizedMatrix e) {
                System.out.println("you can only add matched size matrix");
            }

            catch (DidNotProvideMatrixData e) {
                System.out.println("There got to be Rows and Columns and they have to be positive!");
            }

            return null;
        }

The compiler told me I have to catch "DidNotProvideMatrixData" in add

Thanks a bunch!

Upvotes: 0

Views: 52

Answers (1)

Thilo
Thilo

Reputation: 262504

The same rules apply as for exceptions thrown in methods:

  • If they are checked exceptions you have to handle them (catch or re-throw)

  • If they are RuntimeExceptions, you don't have to (but of course, the code will still error out)

If you get an exception from a constructor, no instance has been constructed, so whatever you tried to do with it (such as assigning to a variable) will not have happened (same as with a return value from a method when there is an exception, you don't get that either).

Upvotes: 4

Related Questions