java - is it worth catching an Exception just to close a stream and reset a CharsetDecoder? -


my understanding closing streams in java recommended, not close "requirement". likewise, it's idea, not near requirement, reset charsetdecoder after done using it.

throwing exception makes method clean , readable. catching exception might "recommended", adds little clutter method. so, on professional programming team, "catch , close/reset"? important? here, preferred throw it:

<!-- language: java -->     public static string[] grabdata(url url, charsetdecoder dcdr) throws exception {            list<string> dump = new arraylist<string>();         dcdr.reset();          httpurlconnection conn = (httpurlconnection) url.openconnection();          bufferedreader bufrdr = new bufferedreader(new inputstreamreader(conn.getinputstream(), dcdr));         while (dump.add(bufrdr.readline())) { }          bufrdr.close();         dcdr.reset();          return dump.toarray(new string[0]);     } 

your understanding incorrect. indeed requirement close open streams, because acquire native os resources not freed when object goes out of scope.

as far throwing exceptions, someone has catch , deal it. , should never throw exception -- throw specific exception types may occur.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -