Why doesn't C# have checked exceptions?

Why doesn't C# have checked exceptions?

Rate This
  • Comments 10

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]

Leave a Comment
  • Please add 1 and 7 and type the answer here:
  • Post
  • 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.
  • 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 :

  • 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

        *

        */

    }

  • 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

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

  • I just realised that that comment I made was on a decade-old article xD

  • 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

Page 1 of 1 (10 items)