Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidParameterException or IllegalArgumentException

Tags:

In Java when should you throw IllegalArgumentException and when InvalidParameterException? Coming from a C# background we would have an ArgumentNullException that derived from ArgumentException. If I would want to implement a equivalent ArgumentNullException/ParameterNullException in Java should I extend IllegalArgumentException or InvalidParameterException?

Note: I'm not trying to implement a ArgumentNullException/ParameterNullException this would just provide me with a better understanding if I could match up these with the C# framework.

like image 504
Cornelius Avatar asked Jan 11 '11 08:01

Cornelius


Video Answer


1 Answers

There's no apparent need to subclass those exceptions, I'd use them right away to signal, that a method has been called with illegal arguments. I'd always describe the real cause in the exceptions message part.

java.security.InvalidParameterException is already a subclass of IllegalArgumentException designed for use by the JCA/JCE engine classes (JavaDoc) and I wouldn't use or subclass it in a different context.

like image 187
Andreas Dolk Avatar answered Oct 07 '22 08:10

Andreas Dolk