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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -