6

What are the constructor parameters for the InvalidParameterValueException class?

InvalidParameterValueException e = new InvalidParameterValueException('param1', 'param2');
e.setMessage('parameter bookId is null');

What are param1 and param2 ?

I cannot find any detailed documentation, there only seems to be Exception Class and Built-In Exceptions article in the Apex Developer Guide.

FYI - I am throwing standard built-in exceptions, because I read here it was OK these days.

2 Answers 2

9

It's Java, not Apex, but here was some documentation (since removed) on a version of InvalidParameterValueException which suggests the first argument is the name of the parameter, and the second is its value:

Field Summary

Fields

|============================================================|
|    Modifier and Type         |    Field and Description    |
|==============================|=============================|
|    static java.lang.String   |    PARAM_NAME               |
|                              |    The default value of     |
|                              |    the parameter name.      |
|------------------------------------------------------------|
|    static java.lang.String   |    PARAM_VAL                |
|                              |    The default value of     |
|                              |    the parameter value.     |
|============================================================|
2
  • Thanks, as a rule of thumb should I fall back to Java documentation if I cannot find any decent Apex documentation, or is that not a reliable strategy?
    – Robs
    May 30, 2018 at 16:43
  • 1
    It's a very case-by-case kind of thing. Like date formatting it's almost exact. Exceptions seem similar. But other functionality may not translate as well.
    – Adrian Larson
    May 30, 2018 at 16:43
2

I use the first param as the name of the method being called and the second for the parameter value that's invalid. In other languages that's super useful for debug purposes. However, if you do a System.debug(e); in your code you'll notice it just prints Script-thrown exception. So I'm not sure how helpful the two parameters really are here. I always add an additional line as well:

ex.setMessage('ABC is not valid for XYZ');

to help with actual debugging.

But I still use method name & variable value out of habit (and the hope that in later versions the toString will be overridden to be more useful).

1
  • Thanks, I was also using setMessage('useful message') but really wanted to know what the parameters were.
    – Robs
    May 30, 2018 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .