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 36155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:20:56+00:00 2026-05-10T14:20:56+00:00

I know two approaches to Exception handling, lets have a look at them. Contract

  • 0

I know two approaches to Exception handling, lets have a look at them.

  1. Contract approach.

    When a method does not do what it says it will do in the method header, it will throw an exception. Thus the method "promises" that it will do the operation, and if it fails for some reason, it will throw an exception.

  2. Exceptional approach.

    Only throw exceptions when something truly weird happens. You should not use exceptions when you can resolve the situation with normal control flow (If statements). You don’t use Exceptions for control flow, as you might in the contract approach.

Lets use both approaches in different cases:

We have a Customer class that has a method called OrderProduct.

contract approach:

class Customer {      public void OrderProduct(Product product)      {            if((m_credit - product.Price) < 0)                   throw new NoCreditException("Not enough credit!");            // do stuff       } } 

exceptional approach:

class Customer {      public bool OrderProduct(Product product)      {           if((m_credit - product.Price) < 0)                    return false;           // do stuff           return true;      } }  if !(customer.OrderProduct(product))             Console.WriteLine("Not enough credit!"); else    // go on with your life 

Here I prefer the exceptional approach, as it is not truly Exceptional that a customer has no money assuming he did not win the lottery.

But here is a situation I err on the contract style.

Exceptional:

class CarController {      // returns null if car creation failed.      public Car CreateCar(string model)      {          // something went wrong, wrong model          return null;      }  } 

When I call a method called CreateCar, I damn wel expect a Car instance instead of some lousy null pointer, which can ravage my running code a dozen lines later. Thus I prefer contract to this one:

class CarController {            public Car CreateCar(string model)      {          // something went wrong, wrong model          throw new CarModelNotKnownException("Model unkown");           return new Car();      }  } 

Which do style do you use? What do you think is best general approach to Exceptions?

  • 1 1 Answer
  • 4 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-10T14:20:56+00:00Added an answer on May 10, 2026 at 2:20 pm

    I favor what you call the ‘contract’ approach. Returning nulls or other special values to indicate errors isn’t necessary in a language that supports exceptions. I find it much easier to understand code when it doesn’t have a bunch of ‘if (result == NULL)’ or ‘if (result == -1)’ clauses mixed in with what could be very simple, straightforward logic.

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

Sidebar

Related Questions

Please Let me know if any best approach available for these two SSIS Activities.
I know two ways to check parameters of the method and throw exceptions when
I know two methods of copying a postgres database, but both of them require
I know of two methods which are not reliable: _access() - doesn't work on
Following are the two approaches: constructor with all the class properties Pros: I have
Help Perlers! Does anyone know a simple insert code here approach would bring code
I see two approaches to handling sender objects in IBAction statements. The first looks
I know that there at least two approaches to leverage the web.config file: using
I know two arrays can be zipped and the result can be iterated with
As we know two Set instances are equal iff they contain the same elements

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.