Maha

Maha

  • NA
  • 0
  • 324.2k

C# ExceptionDemo program

Mar 22 2007 4:17 PM

In this program expected output is as follows:

My flavor - Vanilla

My flavor - Chocolate

But program gives following output:

This is not my flavor

My flavor - Vanilla

This is not my flavor

My flavor - Chocolate

Please explain.

using System;

public class ExceptionDemo

{

public static void Main()

{

MyFlavor i1 = new MyFlavor("Vanilla");

MyFlavor i2 = new MyFlavor("Chocolate");

Console.WriteLine("My flavor - {0}", i1.GetFlavor());

Console.WriteLine("My flavor - {0}", i2.GetFlavor());

}

}

class MyFlavor

{

string flavor;

public MyFlavor(string flavor)

{

this.flavor = flavor;

}

public string GetFlavor()

{

try

{

if(flavor != "Vanilla" || flavor != "Chocolate")

{

MyFlavorException me = new MyFlavorException();

throw(me);

}

}

catch(Exception e)

{

Console.WriteLine(e.Message);

}

return flavor;

}

}

public class MyFlavorException : ApplicationException

{

private static string msg = "\nThis is not my flavor";

public MyFlavorException() : base(msg)

{

}

}




 


Answers (2)