From: Gareth R. <g_j...@ya...> - 2006-01-22 21:03:55
|
Hi, I notice from reading your code that you do not handle exceptions in a safe way. Serious errors need to reported to whatever code is using this client. If execution cannot continue because of an outright failure, that needs to be "admitted" immediately while the execution flow is still near to the problem, not glossed over by catching the exception :-) For instance, in: java-player/javaclient/PlayerDevice.java revision 1.4, you do this: protected synchronized void readHeader() { try { t_sec = is.readInt (); [snip] } catch (Exception e) { System.err.println ("[PlayerDevice] : Error when reading header: " + e.toString ()); } } If any exception occurs then mysterious failures will occur later in the application with little indication as to what caused them. The "rules" in Java are: 1. Throw checked exceptions for conditions that the caller could reasonably be expected to handle e.g. java.io.FileNotFoundException. Wouldn't it be bad if a method hid this exception and a user only found out much later that their data hadn't been processed correctly? 2. Throw unchecked exception for programming errors or conditions that are completely unrecoverable. e.g. A java.lang.NegativeArrayIndexException is normally end of program - it is clearly the fault of the programmer. 3. Don't "catch Exception" - it's too broad and you might catch something that should be propagated back to the caller. From these rules, I would rewrite this method (and all the others), to throw a custom exception e.g. PlayerException. protected synchronized void readHeader() throws PlayerException { try { t_sec = is.readInt (); t_usec = is.readInt (); ts_sec = is.readInt (); ts_usec = is.readInt (); reserved = is.readInt (); size = is.readInt (); } catch (IOException e) // Only one that we need to catch { // Allow the caller to handle this exception, if it can, and store the cause. throw new PlayerException("[PlayerDevice] : Error when reading header", e); } } Personally, from what I've seen so far of the code I'd make the PlayerException a checked exception, i.e. public class PlayerException extends Exception. That's because it looks to me like reestablishing a connection would be a sensible way in which a client could recover (i.e. there is a way). However, that will change the published API (for the better). If you instead make it an unchecked exception then it will extend RuntimeException. As it currently stands I think that this exception hiding is a significant limitation of the library, but if you're using Eclipse, as you've said you are, then I don't think this will take too long to refactor and will provide real benefits :-) Yours, Gareth Randall ___________________________________________________________ To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com |
From: Radu B. R. <ru...@cs...> - 2006-01-23 09:13:54
Attachments:
smime.p7s
|
Hi Gareth, First of all, thanks for the e-mail. Your idea sounds rather good, and I think it should be put somewhere on the top "to do" things for the next version of Javaclient, including the ability to handle Player 2.0. However, I'm a bit caught up with another research project at the moment, and I can't really do much coding for Javaclient, at least until Player 2.0 is officially out. I was wondering if you would like to implement the exception handling yourself, with help from me, or any other members of the community. I can be at your disposal with information about the Javaclient internal structures, provided that you need any. Best regards, Radu. -- | Radu Bogdan Rusu | http://rbrusu.com/ | http://www9.cs.tum.edu/people/rusu/ | Intelligent Autonomous Systems | Technische Universitaet Muenchen Gareth Randall wrote: >Hi, > >I notice from reading your code that you do not handle exceptions in a >safe way. Serious errors need to reported to whatever code is using >this client. If execution cannot continue because of an outright >failure, that needs to be "admitted" immediately while the execution >flow is still near to the problem, not glossed over by catching the >exception :-) For instance, in: >java-player/javaclient/PlayerDevice.java revision 1.4, you do this: > > protected synchronized void readHeader() { > try { > t_sec = is.readInt (); >[snip] > } catch (Exception e) { > System.err.println ("[PlayerDevice] : Error when reading >header: " + e.toString ()); > } > } > >If any exception occurs then mysterious failures will occur later in >the application with little indication as to what caused them. The >"rules" in Java are: > >1. Throw checked exceptions for conditions that the caller could >reasonably be expected to handle e.g. java.io.FileNotFoundException. >Wouldn't it be bad if a method hid this exception and a user only found >out much later that their data hadn't been processed correctly? > >2. Throw unchecked exception for programming errors or conditions that >are completely unrecoverable. e.g. A >java.lang.NegativeArrayIndexException is normally end of program - it >is clearly the fault of the programmer. > >3. Don't "catch Exception" - it's too broad and you might catch >something that should be propagated back to the caller. > > >>From these rules, I would rewrite this method (and all the others), to >throw a custom exception e.g. PlayerException. > > protected synchronized void readHeader() throws PlayerException { > try { > t_sec = is.readInt (); > t_usec = is.readInt (); > ts_sec = is.readInt (); > ts_usec = is.readInt (); > reserved = is.readInt (); > size = is.readInt (); > } catch (IOException e) // Only one that we need to catch > { > // Allow the caller to handle this exception, if it can, >and store the cause. > throw new PlayerException("[PlayerDevice] : Error when >reading header", e); > } > } > >Personally, from what I've seen so far of the code I'd make the >PlayerException a checked exception, i.e. public class PlayerException >extends Exception. That's because it looks to me like reestablishing a >connection would be a sensible way in which a client could recover >(i.e. there is a way). However, that will change the published API (for >the better). If you instead make it an unchecked exception then it will >extend RuntimeException. > > >As it currently stands I think that this exception hiding is a >significant limitation of the library, but if you're using Eclipse, as >you've said you are, then I don't think this will take too long to >refactor and will provide real benefits :-) > >Yours, > >Gareth Randall > > > > >___________________________________________________________ >To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com > > >------------------------------------------------------- >This SF.net email is sponsored by: Splunk Inc. Do you grep through log files >for problems? Stop! Download the new AJAX search engine that makes >searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! >http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642 >_______________________________________________ >Java-player-developers mailing list >Jav...@li... >https://lists.sourceforge.net/lists/listinfo/java-player-developers > > |
From: Gareth R. <g_j...@ya...> - 2006-01-29 21:38:38
|
Hi Radu, I'll see what I can do over the next 2 weeks, and send CVS diffs :-) Does anyone on the list have views as to whether exceptions arising from calling javaclient methods should be checked or unchecked exceptions? Most of the issues that are being caught look rather serious - sufficiently serious that most callers of the code will not want to try to handle them. E.g. if something is serious enough for you to be calling System.exit(1) then it's unlikely that a client will be able to recover ! This suggests that they should be unchecked (i.e. extend RuntimeException). If a caller really wants to then they can catch them, but otherwise they need not be burdened with the extra code. A further argument for unchecked comes from SQLException. One of the (many) criticisms of "java.sql.SQLException" is that most database errors are simply unrecoverable without human intervention, and that there is no point in a caller being forced to catch them. This argument seems to apply to javaclient. Unchecked is best unless someone can think of a compelling reason otherwise. Yours, Gareth Randall ___________________________________________________________ To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com |
From: Radu B. R. <ru...@cs...> - 2006-01-30 10:21:17
Attachments:
smime.p7s
|
Hey Gareth, Thanks. I know that all Javaclient users will appreciate this. :) I am slowly preparing to start coding the diffs for handling Player 2.0. In the meantime, I have coded a new interface (RFID) for Player and the appropriate code for Javaclient and the C/C++ client. I will post the patches here and on my web page as soon as possible, and with a little bit of luck, I shall be able to convince Brian to merge them with the Player2 repo, once it's stabilized. Right now I am handling two brands of RFID readers, but I'm sure that once the code is out, people will add support for their own. If anyone here is working with RFID and has any requests on what this interface should contain, please speak now. For beginning, I'm adding Select and then Read/Write support to the tags, but we might make use of encryption functions later too, if anybody needs them (Inside readers have a bunch of techniques for that). PS. Gareth, you might want to CC e-mails related to this subject to java-player-users@sf, since I know for a fact that some of our users aren't subscribed to -developers, and vice-versa. Thanks. Best regards, Radu. -- | Radu Bogdan Rusu | http://rbrusu.com/ | http://www9.cs.tum.edu/people/rusu/ | Intelligent Autonomous Systems | Technische Universitaet Muenchen Gareth Randall wrote: >Hi Radu, > >I'll see what I can do over the next 2 weeks, and send CVS diffs :-) > >Does anyone on the list have views as to whether exceptions arising >from calling javaclient methods should be checked or unchecked >exceptions? > >Most of the issues that are being caught look rather serious - >sufficiently serious that most callers of the code will not want to try >to handle them. E.g. if something is serious enough for you to be >calling System.exit(1) then it's unlikely that a client will be able to >recover ! > >This suggests that they should be unchecked (i.e. extend >RuntimeException). If a caller really wants to then they can catch >them, but otherwise they need not be burdened with the extra code. > >A further argument for unchecked comes from SQLException. One of the >(many) criticisms of "java.sql.SQLException" is that most database >errors are simply unrecoverable without human intervention, and that >there is no point in a caller being forced to catch them. This argument >seems to apply to javaclient. > > >Unchecked is best unless someone can think of a compelling reason >otherwise. > >Yours, > >Gareth Randall > > |
From: Gareth R. <g_j...@ya...> - 2006-02-01 15:07:51
|
Note to list: I've sent Radu a GNU tar.gz of my changes. This is what I've done: 1. Changed all occurrences of catch(Exception e) to only catch the specific exception that can be thrown. 2. Changed all cases of catch(Exception e) followed by a System.err.println to throw an exception instead. The caller can log the exception if desired. 3. Rewrote dangerous catching of NullPointerException. 4. Handled InterruptedException in Thread methods. Yours, Gareth Randall ___________________________________________________________ To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com |
From: Radu B. R. <ru...@cs...> - 2006-02-01 16:26:39
Attachments:
smime.p7s
|
Hi Gareth, Got your mail. Thanks ;) I will look at it and try to include the changes asap. I just posted the RFID patch to http://www9.cs.tum.edu/people/rusu/playerstage/ and sent a message to the ps mailing lists. If anybody has problems running it or building Javaclient applications that use the RFIDInterface, please let me know. Best regards, Radu. Gareth Randall wrote: >Note to list: > >I've sent Radu a GNU tar.gz of my changes. > >This is what I've done: > >1. Changed all occurrences of catch(Exception e) to only catch the >specific exception that can be thrown. > >2. Changed all cases of catch(Exception e) followed by a >System.err.println to throw an exception instead. The caller can log >the exception if desired. > >3. Rewrote dangerous catching of NullPointerException. > >4. Handled InterruptedException in Thread methods. > >Yours, > >Gareth Randall > > -- | Radu Bogdan Rusu | http://rbrusu.com/ | http://www9.cs.tum.edu/people/rusu/ | Intelligent Autonomous Systems | Technische Universitaet Muenchen |