aspose file tools
The moose likes Java in General and the fly likes Throw an IOException or chain/wrap it Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Throw an IOException or chain/wrap it" Watch "Throw an IOException or chain/wrap it" New topic
Author

Throw an IOException or chain/wrap it

Salman Ahmed
Greenhorn

Joined: Mar 18, 2008
Posts: 27
I am developing a class library that provides access to a Server based application over TCP/IP sockets. All functional operations possible through my class library essential invoke a request-response cycle over the underlying socket connection to my server. So, for almost all of these operations an IOException can be generated while sending the request and/or reading the response over the wire.

My question is: is it advisable to just throw an IOException for all my public operations or should I define a custom MyAppException class and chain the IOException to it and have my public operations just throw the MyAppException custom exception class?

Lack of experience with real-world Java programming and lack of knowledge of Java best practices is what's really driving this question!

Thanks for any advice or suggestions.
Regards,
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
My view is that it depends whether IOException will cover all possible future requirements.

As an example, say you define a method getData() that gets some data from somewhere. Currently, you implement it to read a file, so the internal operations can throw IOException. Should you declare getData() to throw IOException or your own exception?

Say you later decide to use a database-based implementation. Then the internal operations will typically throw SQLException. Your declaration of getData() throwing IOException has become inappropriate.

If you think that sort of thing may happen in your application, then defining your own wrapper exception avoids it.

But, if you think that's unlikely, then defining your own wrapper is just bloat.

As with many things, you have to make an educate guess about the future.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Throw an IOException or chain/wrap it
 
Similar Threads
Problem starting servlet
Application exceptions and BMT
should lock method throw DatabaseException ?
Exception propagation in layered applications
Filter Problem