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
  • 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-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

Does anyone know of a simple way to compare two strings together to generate
I have two keyboards connected to my PC, is there any way to know
I have two computers. Both running WinXP SP2 (I don't really know ho similar
I know of two methods of setting a default parameter, but I would like
Currently I know of only two ways to cache data (I use PHP but
To start, I know there are two kinds of JSON serialization currently built into
do you know any good algorithms that match two strings and then return a
I wish to know all the pros and cons about using these two methods.
Do you know of any compilers that only requires one or two clicks on
I have two Jetty AppServers running a Grails Web App behind Apache 2.2 reverse

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.