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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:51:55+00:00 2026-05-17T19:51:55+00:00

Why is it preferable to throw this Exception Throw New DivideByZeroException(You can’t divide by

  • 0

Why is it preferable to throw this Exception

Throw New DivideByZeroException("You can't divide by zero")

over this general one:

Throw New Exception("You can't divide by zero")

What advantage is gained in this particular example? The message already tell it all. Do standard subclasses that inherit from the base Exception class ever have different methods that the base? I haven’t seen a case, but I must admit that I tend to throw the base Exception.

  • 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-17T19:51:55+00:00Added an answer on May 17, 2026 at 7:51 pm

    The type of the exception allows handlers of the exception to filter it. If all you threw were exceptions of type Exception how would handlers know what exceptions to catch and which to allow to propagate up the call stack?

    For example, if you always throw Exception:

    void Foo(string item) {
      try {
        if (Bar(item)) { 
          Console.WriteLine("BAR!");
        }
      } catch (Exception e) {
        Console.WriteLine("Something bad?");
      }
    }
    
    bool Bar(string item) {
      if (item == null) {
        throw new Exception("Argument is null!");
      }
    
      return Int32.Parse(item) != 0;
    }
    

    How does the caller Foo know if a null exception occurred or if the Int32.Parse() failed? It has to check the type of the thrown exception (or do some nasty string comparison).

    It’s even more worrisome if you get a ThreadAbortException or OutOfMemoryException which can occur in spots you wouldn’t expect an exception. In these cases if your catching code only catches Exception you may mask these (important) exceptions and cause damage to your program (or system) state.

    The example code should read:

    void Foo(string item) {
      try {
        if (Bar(item)) { 
          Console.WriteLine("BAR!");
        }
      } catch (ArgumentNullException ae) {
        Console.WriteLine("Null strings cannot be passed!");
      } catch (FormatException fe) {
        Console.WriteLine("Please enter a valid integer!");
      }
    }
    
    bool Bar(string item) {
      if (item == null) {
        throw new ArgumentNullException("item");
      }
    
      return Int32.Parse(item) != 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MyClass[] array; List<MyClass> list; What are the scenarios when one is preferable over the
In SQL Server, why is this: [dbo].[table_name] preferable to this: dbo.table_name And along those
What is preferable, keeping a dataset in session or filling the dataset on each
We have always had languages that were preferable to be used in a particular
When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of
Does anyone know of a free Perl program (command line preferable), module, or anyway
How to calculate the length (in pixels) of a string in Java? Preferable without
I'm trying to write a greasemonkey script, and it would be preferable for it
Preferably in VB.Net, but C# is fine, how can I access the extra properties
Edit This question has gone through a few iterations by now, so feel free

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.