Is there any point catching an out of memory error (java.lang.OutOfMemoryError
) in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The golden rule is to only catch errors that you can handle. If you can do something useful after an OutOfMemory error, then go ahead.
No, catch
Exception
andRuntimeException
, but hardly ever (changed from 'never')Error
:Note:
I'm quoting the official Javadocs here. If you don't agree, tell Oracle, don't shoot the messenger :-)
If you want to have a graceful shutdown which handles this case specificly.
You can also use it if you may have to allocate a large array and you want to gracefully degrade your system.
EDIT: An example of code where I used to check OOM if the stream was corrupted. I have since replace the len check to ensure the len is between 0 and 16 MB instead.