java - when and how will ImageIO.write throw nullpointerexception? -
in
imageio.write(renderedimage,string,file)
method, imageoutputstream created through
stream = createimageoutputstream(output);.
in createimageoutputstream runtime exception caught , returns null catch block.
try { iter = theregistry.getserviceproviders(imageoutputstreamspi.class,true); } catch (illegalargumentexception e) { return null; }
can me understand:
- what reason behind catching runtime exception here?(unless crappy coding)
- on condition, code throw illegal argument exception? (i dont see reason throw)
please help.
it case of code has evolved on time , no 1 cared clean up, or implementers of imageio
relied on contract of serviceregistry
, rather implementation details of it. in current implementation of imageio
, serviceregistry
don't see how ever happen.
the thing can see it, potentially change implementation of iioregistry
or serviceregistry
lazy loading of categories example, or perhaps flush out categories no service providers (spi) present. contracts of methods not break, , imageio
class still work does.
now, question in question, answer should never throw nullpointerexception
. *
if want to, stream
null
, de-registering spi file
(fileimageoutputstreamspi
). then, well-behaved imagewriter
should throw illegalstateexception
indicating output has not been set, not nullpointerexception
(of course, programming errors cause npes in other places, not normal flow).
update:
*) op himself pointed out in linked answer, throwing of nullpointerexception
can indeed happen if pass file
object pointing non-existing path. happens because fileimageoutputstreamspi
"swallows" ioexception
caused randomaccessfile
constructor (it print stacktrace standard out), , returns null
. in opinion, breaks contract in more severe way letting ioexception
bubble up, or wrapping in illegalargumentexception
original exception cause , explanatory message. consider filing bug report, if doesn't exist 1 issue.
Comments
Post a Comment