Partager via


Why doesn't C# have checked exceptions?

Checked exceptions are a very hotly debated topic in some circles, particularly for experienced Java developers moving to, or additionally learning, C#. Here are some resources that discuss the issue in depth:

Note that without the CLR itself supporting checked exceptions, it would be effectively impossible for C# to do so alone.

[Author: Jon Skeet]

Comments

  • Anonymous
    March 12, 2004
    AFAIK is the Java compiler that enforces checked exceptions and not JVM. You can emit byte code that will totally ignore any checked exception.

    So (in theory), the C# compiler could behave exactly as Java (using some special attributes)

    but IMHO leaving checked exceptions out of C# and CLR was an excellent choice.

  • Anonymous
    June 08, 2008
    From an Email conversation this evening: what do you think about it? For a while, I was "all flame" about Checked exceptions, but then I read about their limitations (which are why they aren't included in C#, more about it there :

  • Anonymous
    July 14, 2013
    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace polymorphisum {    class A    {        public virtual void show()        {            Console.WriteLine("Base Class function");        }    }    class b : A    {        public virtual void show()        {            Console.WriteLine("Drived Class function");        }    }    class c : b    {        public override void show()        {            Console.WriteLine("C Class function");        }        static void Main(string[] args)        {            c c1 = new c();            c1.show();        }    }    /*     * A C1 = new C();     * Question 1. How to Call C Class Show Function in this Condition     * Question 2. How to Call B Class Show Function in this Condition     * Question 3. Why Alway Call A Class Show Function in this Condition. if i give virutal keyword or not     * -------------------------------------------------------------------------     * Second Senario     * B C1 = new C();     * Question 1. How to Call A Class Show()/Function in this Condition     * Question 2. Why C Class  Show()/function Call here     * Question 3. Why B Class Show()/function Call if i have A Class show() and both in class i am use virtual keyword means     * why not call A Class Show()/Function     * --------------------------------------------------------------------------     * Thired  Senario     * C C1 = new C();     * Question 1. Why Alway call C Class Show()/Function if am useing or not use the override keyword     * Question 2. How to Call A Class Show()/Function in this Condition     * Question 3. How to Call B Class Show()/Function in this Condition     *     * Give me answer on Himanshu888.sharma@gmail.com or Himanshu888.sharma@facebook.com     * thanx in advance     *     */ }

  • Anonymous
    October 14, 2013
    protected void AddRecord(object sender, EventArgs e)    {        float CalculatedQty = Int64.Parse(TextBox4.Text);        float PhysicalQty = Int64.Parse(txt1.Text);        DateTime StTakdt = Convert.ToDateTime(txtdt1.Text);        bal pBAL = new bal();        int intResult = 0;        try        {            intResult = pBAL.Insert5(int.Parse(TextBox5.Text), int.Parse(TextBox2.Text), CalculatedQty, PhysicalQty, StTakdt);            if (intResult > 0)                lbl.Text = "Record Insert Successfully.";            else                lbl.Text = lbl.Text = "AuditID [<b>" + TextBox5.Text + "</b>] alredy exists, try another name";            throw new Exception("plz enter detail");        }        catch (Exception ee)        {            Console.WriteLine("{0} Exception caught plz enter detail.", ee);            lbl.Text = ee.Message.ToString();        }        finally        {            pBAL = null;        }        gvdetails.EditIndex = -1;        // Refresh the list        BindGrid();    } i click on add button  without inserting anything it will throw exception plz enter detail

  • Anonymous
    March 25, 2014
    "Note that without the CLR itself supporting checked exceptions, it would be effectively impossible for C# to do so alone." No it wouldn't. You could have a CheckedException class that inherits from Exception, and then run a compile time check to see if any CheckedExceptions that may be thrown are caught. Although I'm more of the opinion that the ideal set-up is to note exceptions that may be thrown in the documentation and fire warnings then those exceptions aren't either caught or redeclared in the documentation. Checked exceptions ala Java either force you to restrict yourself to the exceptions you originally declared, or break code that calls a method that may throw an exception.

  • Anonymous
    March 25, 2014
    I just realised that that comment I made was on a decade-old article xD

  • Anonymous
    April 30, 2014
    The basic design philosophy of C# is that actually catching exceptions is rarely useful, whereas cleaning up resources in exceptional situations is quite important. I think it's fair to say that using (the IDisposable pattern) is their answer to checked exceptions csharp.net-informations.com/.../csharp-exceptions.htm lev

  • Anonymous
    September 27, 2014
    The comment has been removed

  • Anonymous
    July 31, 2015
    Third link is dead, any possible update?

  • Anonymous
    October 29, 2015
    Archive of the third link (Why doesn't C# have exception specifications?) is available here: web.archive.org/.../aa336812.aspx