Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:42:22+00:00 2026-05-11T01:42:22+00:00

I keep getting stuck conceptually on deciding an Exception-handling structure for my project. Suppose

  • 0

I keep getting stuck conceptually on deciding an Exception-handling structure for my project.

Suppose you have, as an example:

public abstract class Data {    public abstract String read(); } 

And two subclasses FileData, which reads your data from some specified file, and StaticData, which just returns some pre-defined constant data.

Now, upon reading the file, an IOException may be thrown in FileData, but StaticData will never throw. Most style guides recommend propagating an Exception up the call stack until a sufficient amount of context is available to effectively deal with it.

But I don’t really want to add a throws clause to the abstract read() method. Why? Because Data and the complicated machinery using it knows nothing about files, it just knows about Data. Moreover, there may be other Data subclasses (and more of them) that never throw exceptions and deliver data flawlessly.

On the other hand, the IOException is necessary, for if the disk is unreadable (or some such) an error must be thrown. So the only way out that I see is catching the IOException and throwing some RuntimeException in its place.

Is this the correct philosophy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:42:22+00:00Added an answer on May 11, 2026 at 1:42 am

    You’re right.

    The exception should be at the same level of abstraction where is used. This is the reason why since java 1.4 Throwable supports exception chaining. There is no point to throw FileNotFoundException for a service that uses a Database for instance, or for a service that is ‘store’ agnostic.

    It could be like this:

    public abstract class Data {    public abstract String read() throws DataUnavailableException; }  class DataFile extends Data {      public String read() throws DataUnavailableException {         if( !this.file.exits() ) {             throw new DataUnavailableException( 'Cannot read from ', file );          }           try {                ....          } catch( IOException ioe ) {               throw new DataUnavailableException( ioe );          } finally {               ...          }  }   class DataMemory extends Data {      public String read()  {         // Everything is performed in memory. No exception expected.     }  }   class DataWebService extends Data {        public string read() throws DataUnavailableException {            // connect to some internet service            try {               ...            } catch( UnknownHostException uhe ) {               throw new DataUnavailableException( uhe );            }       }  } 

    Bear in mind that if you program with inheritance in mind, you should design carefully for the specific scenarios and test implementations with those scenarios. Obviously if it is harder to code an general purpose library, because you don’t know how is it going to be used. But most of the times applications are constrained to an specific domain.

    Should your new exception be Runtime or Checked? It depends, the general rule is to throw Runtime for programming errors and checked for recoverable conditions.

    If the exception could be avoided by programming correctly ( such as NullPointerException or IndexOutOfBounds ) use Runtime

    If the exception is due to some external resource out of control of the programmer ( the network is down for instance ) AND there is something THAT could be done ( Display a message of retry in 5 mins or something ) then a checked exception should be used.

    If the exception is out of control of the programmer, but NOTHING can be done, you could use a RuntimeException. For instance, you’re supposed to write to a file, but the file was deleted and you cannot re-create it or re-try then the program should fail ( there is nothing you can do about it ) most likely with a Runtime.

    See these two items from Effective Java:

    • Use checked exceptions for recoverable conditions and run-time exceptions for programming errors
    • Throw exceptions appropriate to the abstraction

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The problem with Database logging isn't the speed: it's the… May 11, 2026 at 12:23 pm
  • added an answer The left self-join was a good instinct, but I don't… May 11, 2026 at 12:23 pm
  • added an answer I, too, encountered that problem this evening. After some searching… May 11, 2026 at 12:23 pm

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I keep getting compiler errors when I try to access flashVars in an AS3
I'm developing an ASP.Net MVC site and on it I list some bookings from
I'm new to Flex SDK and trying to implement a simple project using Doug

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.