-1

I receive a long value from C (that is from JNI/JNA). I want to create a byte[] array of that size.

Which exception should I throw if this value exceeds the maximum size of arrays?

In other words, which exception should I throw in the following?

long output_len;
...
if(output_len > Integer.MAX_VALUE)
    throw new ...();
porton
  • 5,214
  • 11
  • 47
  • 95

1 Answers1

2

Java itself throws OutOfMemeryError in case of trying to assign too big integer to size of an array, but - as suggested by @StephenC - you shouldn't make your code throw this kind of error. You should rather consider creating your own Exception class and provide specific message in its body as you decided to throw exception in case of output_len > Integer.MAX_VALUE. IllegalArgumentException seems to be a good idea too, but it is worth considering to provide a specific message describing why this Exception is thrown.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • A program should not be throwing OOMEs ... **unless** you don't expect the caller to ever catch them. An OOME is an `Error` and should be treated as a fatal error. Signalling a (possibly) recoverable error condition with an `Error` is just wrong. – Stephen C Jan 30 '18 at 22:45
  • @StephenC So if I don't misunderstand you, when creating your own classes, constructors and methods, there shouldn't be any possibility to throw such an Error? So i think that there is no such an Exception suitable for case presented by OP (except his own exception) - maybe IllegalArgumentException, but I think it's not that accurate actually. What do you think of it? – Przemysław Moskal Jan 30 '18 at 22:57
  • @StephenC I disagree. There are logically 3 reasons for `new xxx[output_len]` to fail: 1) Not enough free memory, 2) Exceeds VM limit, 3) Value of `output_len` exceeds `int` range. The first two throw `OutOfMemoryError`. Why should the error for #3 be different? It's really the same issue: You asked for more than can be done. Sure, the description (`message`) should clarify reason, but the error is the same. – Andreas Jan 30 '18 at 23:02
  • 2
    Not exactly. What I am saying is that you should never design an API that **requires** a caller to catch an `Error`. `IllegalArgumentException` is a reasonable choice, IMO. If the OP requires something specific he can declare his own custom exception. – Stephen C Jan 30 '18 at 23:02
  • @Andreas Of course I'm not that experienced as both of you are, but I think that 1) and 2) of your examples are rather independent to code that will be provided (it depends on internal things in VM or in memory) when the 3) is more dependent of the intentions of class designer - he decides on his own that this size of array is inappropriate - so I'm not sure if Error is a correct way of handling this issue. – Przemysław Moskal Jan 30 '18 at 23:09
  • @PrzemysławMoskal Class designer doesn't *decide* that this size of array is inappropriate. There is no decision to make, since the size is impossible in Java. An array cannot have more than `Integer.MAX_VALUE` number of elements, since array size is an `int`. Class designer might choose *which* exception should be thrown from method when size is too large, but that exception should apply to all 3 cases. If class designer *chooses* something other than `OutOfMemoryError`, then code need to catch it and throw the chosen exception. – Andreas Jan 31 '18 at 16:10
  • @Andreas Of course you are right. I think I confused presented situation with the one that you check array size acceptable by JVM and by heap size, but not acceptable by designer due to any circumstances that are not suitable in the case he thinks of, so I'm sorry for that. In that case class designer could perform behavior that suits him the most. – Przemysław Moskal Jan 31 '18 at 18:21