java - Adding 'throws' on an extended class? -


i'm making simple application socket api, have run small issue haven't found answer for.

the application supposed multi-threaded, main method starts both server , client threads.

public class udpclientserver1 extends thread 

however, since the socket classes need throw specific exceptions, must this:

public void runclient() throws socketexception, unknownhostexception, ioexception 

and runserver() method.

not there's wrong that, there more elegant solution? along lines of :

public class udpclientserver1 extends thread, throws exception 

ideally may want wrap exception throw, either converting runtimeexception if error unrecoverable, or @ least more appropriate / encapsulated exception.

e.g.

public void runclient throws clientexception {     try     {         //     } catch (exception e)     {         log.error("exception encountered in client",e);         throw new clientexception("unrecoverable client exception encountered",e);         // or: throw new runtimeexception("unrecoverable client exception",e);     } } 

clientexception in above own exception need create. purpose encapsulate implementation details of corresponding exceptions calling code.

using runtimeexception, however, may more appropriate if there no reasonable way calling code respond exception. when throwing runtimeexception not have declare in method signature , calling code not need explicitly handle (although should handled @ point).


Comments