What type of exception should I use?

I am writing a program that will perform operations with matrices, and I am trying to figure out which use should I use in case of invalid measurements. Is there any type of exception that already exists that would be acceptable for my operations to throw; or should I implement my own exception type? I know that almost any type of exception will do what I want, but the problem is that the exception really describes the problem that caused it.

+5
source share
4 answers

What you are looking for comes closest to IndexOutOfBoundsException. You can use it as is or deduce from it your own exception MatrixIndexOutOfBoundsException.

+7
+2
+2

If you can use your own exception. then your own exception should extend java.lang.RuntimeException or a subclass of RuntimeException. RuntimeException is an exception without checking. In this case, you should use the exception without checking.

0
source

All Articles