c# - Is throwing OutOfMemoryException from application code good design -
i remeber read somewhere throwing of system.systemexception
derived exceptions user code bad practice, user code should throw application exceptions.
we have native library uses gpu. may return error code indicates we`re out of gpu memory. want translate error code .net exception.
this possible exceptions can think of:
system.outofmemoryexception
system.invalidoperationexception
appropiate text- custom exception (based on invalidoperationexception)
which 1 best , why?
raising outofmemoryexception
worst choice. reason this exception impossible recover in general case , client, extremely hard determine whether system ran out of memory or abused exception type, because client not expect behavior.
raising invalidoperationexception
better thing, because client able handle (by offloading computation somewhere else or performing required stuff in cpu, potentially using different library). however, if exception invalidoperationexception
, exception type used other libraries or used bcl. thus, client has react exception parsing error message, not reliable.
therefore, best solution custom exception type, because enable clients catch exception type , recover situation or tell user problem is. whether or not feel special case of invalid operation you. personally, not let exception inherit invalidoperationexception
exception
directly.
Comments
Post a Comment