2

Thank you all for helping.

This code doesn't produce what I expect when the divisor is 1. The base class for ExceptOne doesn't get called, the hyperlink in ExceptOne doesn't get displayed. What am I missing ?!

Console output is:

enter a divisor
1
WriteLine exception 1...
WriteLine exception 2...
base ctor2
http : // exc2.com
Writeline in finally

class Program
{
    static void Main(string[] args)
    {
        try
        {
            byte y = 0;
            byte x = 10;
            Console.WriteLine("enter a divisor");
            string s = (Console.ReadLine());
            y = Convert.ToByte(s);
            if (y == 1) throw new ExceptOne();
            Console.WriteLine("result is {0}", x / y); ;
        }

        catch (System.DivideByZeroException e)
        {
            Console.WriteLine("exception occured {0}...", e.Message);
        }

        catch (ExceptOne p)
        {
            Console.WriteLine(p.Message +"\n"+ p.HelpLink);

        }

        catch (System.Exception r)
        {
            Console.WriteLine(r.Message + "\n" + r.HelpLink);
        }

        finally
        {
            Console.WriteLine("Writeline in finally ");
            Console.ReadLine();
        }
    }
}

public class ExceptOne : System.Exception
{
    public ExceptOne()
        : base("base ctor 1 ")
    {
        this.HelpLink = "http://exc1.com";
        Console.WriteLine("WriteLine exception 1...");
        throw new Exception2();
    }
}

public class Exception2 : System.Exception
{
    public Exception2()
        : base("base ctor2 ")
    {
        Console.WriteLine("WriteLine exception 2...");
        this.HelpLink = "http://exc2.com";
    }
}
Ray
  • 45,695
  • 27
  • 126
  • 169
steelponey
  • 55
  • 1
  • 5

3 Answers3

4

You are throwing an exception in the constructor of the ExceptOne exception. So an ExceptOne object will never be created and the catch for that exception will not be triggered.

EDIT

It can be OK to throw an exception in a constructor. See: http://bytes.com/topic/c-sharp/answers/518251-throwing-exception-constructor and When is it right for a constructor to throw an exception?

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115
  • Thanks, just learning this stuff. However, Writeline statement does get executed in that constructor... – steelponey Jan 12 '12 at 09:49
  • Of course, and right after that you throw the exception... Throwing an exception in a constructor will not prevent the code from being executed (up to the throw) but it will prevent the object from being created. – Emond Jan 12 '12 at 09:51
  • yes; try commenting throw new Exception2(); .. why did you write that? – Emanuele Greco Jan 12 '12 at 10:02
  • I guess the question I wanted to ask also, is throwing an exception in a constructor a bad idea, should this be avoided ? – steelponey Jan 17 '12 at 01:13
  • It is entirely possible. See: http://bytes.com/topic/c-sharp/answers/518251-throwing-exception-constructor and http://stackoverflow.com/questions/77639/when-is-it-right-for-a-constructor-to-throw-an-exception – Emond Jan 17 '12 at 05:18
1

If you see that when you raise the ExceptOne exception in the constructor you throw a new Exception2 type of exception which is not caught in your Main(...) method and hence it gets caught in the general exception clause.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
Rajesh
  • 7,766
  • 5
  • 22
  • 35
0

This happens because you throwing Exception2 in ExceptOne causing Exception2 to be caught in your main method by the (System.Exception r) block.

The base for ExceptOne is called, the message (set by base("base ctor 1 ")) is just never displayed because that exception is never caught.

maka
  • 566
  • 4
  • 11