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

  • SEARCH
  • Home
  • 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 6201497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:33:55+00:00 2026-05-24T04:33:55+00:00

I’m completely tired of all the buggy code I’ve been writing for all this

  • 0

I’m completely tired of all the buggy code I’ve been writing for all this time and so it looks like I really need to understand the correct approach for working with exceptions.

Let’s consider the following example:

interface IDataSource
{
  string ReadData(int offset);
}

class FileDataSource : IDataSource {...}
class DatabaseDataSource : IDataSource {...}

If I’m using an object of class that implements IDataSource, what are the exceptions I can expect to catch if ReadData() fails for some reason?

FileDataSource can fail because there’s no file, or file is less than offset. DatabaseDataSource can fail because it can’t connect to the database, or there’s no required table in that database.

When I do this:

var data = dataSource.ReadData(10);

What should I catch? From the other side, if I’m implementing a new class FakeDataSource, what should I throw if something bad happens?

I have a feeling that when I throw FileNotFoundException from FileDataSource or SqlException from DatabaseDataSource it means encapsulation violation, since I know implementation details.

Should all the exceptions thrown by any IDataSource be bound to this interface? What I mean is – when I define any new abstraction, should I also define the related exceptions? Like this:

interface IDataSource { ... }    
abstract class DataSourceException : Exception { ... }

So, when someone decides to implement IDataSource, he should only throw DataSourceExceptions. Is that correct? What about wrong values for offset – should I still throw DataSourceExceptions or standard (.NET) ArgumentOutOfRangeException is OK?

Would also appreciate any links to the articles about error handling you consider worthy.

  • 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. Editorial Team
    Editorial Team
    2026-05-24T04:33:56+00:00Added an answer on May 24, 2026 at 4:33 am

    You should only catch the exceptions that you can handle. If your IDataSource throws FileNotFoundException or SqlException and you can’t handle it, don’t catch it.

    When throwing exceptions, you should throw the type of exception that best describes the problem. There are three things that you want to know from a thrown exception.

    • What went wrong?
    • Where did it go wrong?
    • Why did it go wrong?

    When exceptions are used effectively, what is answered by the type of
    exception thrown, where is answered by the exception stack trace, and
    why is answered by the exception message. If you find your exceptions
    aren’t answering all three questions, chances are they aren’t being
    used effectively. Three rules will help you make the best use of
    exceptions when debugging your programs. These rules are: be specific,
    throw early, and catch late.

    In most cases use commonly defined exceptions rather than creating your own. People will be familiar with them and understand what they mean without additional explanation. However, creating your own exception type may be helpful in cases where it is desirable to encapsulate details for a particular module. When taking this approach, wrap the original exception within the exception for your module so that the information doesn’t get lost when it is rethrown.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is

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.