Reputation: 31
I'm designing a solution, which includes a C++ library and several Java applications using the library via JNI.
C++ library allocates native memory massively. It's possible to detect from C++ code when this allocation fails. Failure to allocate should be reported to Java code with throwing something throwable.
The 2 options are considered:
What would be the right option and why?
Upvotes: 3
Views: 2196
Reputation: 851
OutOfMemoryError
has a specific meaning:
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector
Since it is a native heap allocation that's failing, it would be inappropriate for your code to throw this error. Notwithstanding what ByteBuffer.allocateDirect()
does.
I would recommend that you create your own exception, extending Error
. It should not be a checked exception, as there is little/nothing that a running program could do to avoid the error.
Upvotes: 5