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

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like you are calling getProductIterator() with the starting… May 13, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer There is no strtok in the valid Linux Kernel API.… May 13, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer I think you might be getting your null reference exception… May 13, 2026 at 6:35 pm

Related Questions

Out of order execution in CPUs means that a CPU can reorder instructions to
How do I define a DynamicMethod for a delegate that has an out -parameter,
I'm a Flex rookie tasked with enhancing an existing application. One of those enhancements
I'm interested at what stage in your development do add logging and/or tracing to

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.