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){
}
}

Scott LysleRank #49 · 31K reputationPosted Mar 22, 2007, 12:52 PM EDT
Change:
if(flavor != "Vanilla" || flavor != "Chocolate")
To this:
if(flavor != "Vanilla" && flavor != "Chocolate")
Posted Mar 22, 2007, 1:00 PM EDT
Maha
If you explain the reason much appreciated.