Hallo Community,
ich bin gerade dabei mal zu üben wie ich Eigene Exeptions werfe.
habe dazu folgenden Code geschrieben:
[highlight=c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace KL01_A2
{
public class NegativException : Exception
{
public NegativException() { }
public NegativException(string message) : base(message) { }
public NegativException(string message, Exception inner): base(message, inner) { }
}
public class eins
{
Exception e = new Exception();
protected int wert;
public virtual int Wert
{
get { return wert; }
set
{
if (wert < 0)
{
throw (new NegativException("Unzulässiger Radius",e));
}
else
wert = value;
}
}
}
public class zwei : eins { public override int Wert { get { return 2 * wert; } } }
public class drei : zwei { public override int Wert { get { return 3 * wert; } } }
class Program
{
static void Main(string[] args)
{
int eingabe = 1;
eins a = new eins();
zwei b = new zwei();
drei c = new drei();
while (eingabe != 0)
{
eingabe = int.Parse(Console.ReadLine());
try
{
a.Wert = eingabe;
}
catch (NegativException e)
{
Console.WriteLine(e.Message);
}
a.Wert = eingabe;
c.Wert = eingabe;
Console.WriteLine("{0}, {1}, {2}", a.Wert, b.Wert, c.Wert);
a = c;
b = c;
Console.WriteLine("{0}, {1}, {2}", a.Wert, b.Wert, c.Wert);
}
}
}
}
[/highlight]
Nur wird die Exception nicht geworfen weil ich als Fehlermeldung bekomme:
"NegativException wurde nicht behandelt".
Bei Galileo wurde allerdings nichts anderes gemacht als das was ich gemacht habe. Jedenfalls steht nicht mehr im Openbook und ich habe die dazugehörige Projectdatei nicht um nachzuschauen.
ich bin gerade dabei mal zu üben wie ich Eigene Exeptions werfe.
habe dazu folgenden Code geschrieben:
[highlight=c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace KL01_A2
{
public class NegativException : Exception
{
public NegativException() { }
public NegativException(string message) : base(message) { }
public NegativException(string message, Exception inner): base(message, inner) { }
}
public class eins
{
Exception e = new Exception();
protected int wert;
public virtual int Wert
{
get { return wert; }
set
{
if (wert < 0)
{
throw (new NegativException("Unzulässiger Radius",e));
}
else
wert = value;
}
}
}
public class zwei : eins { public override int Wert { get { return 2 * wert; } } }
public class drei : zwei { public override int Wert { get { return 3 * wert; } } }
class Program
{
static void Main(string[] args)
{
int eingabe = 1;
eins a = new eins();
zwei b = new zwei();
drei c = new drei();
while (eingabe != 0)
{
eingabe = int.Parse(Console.ReadLine());
try
{
a.Wert = eingabe;
}
catch (NegativException e)
{
Console.WriteLine(e.Message);
}
a.Wert = eingabe;
c.Wert = eingabe;
Console.WriteLine("{0}, {1}, {2}", a.Wert, b.Wert, c.Wert);
a = c;
b = c;
Console.WriteLine("{0}, {1}, {2}", a.Wert, b.Wert, c.Wert);
}
}
}
}
[/highlight]
Nur wird die Exception nicht geworfen weil ich als Fehlermeldung bekomme:
"NegativException wurde nicht behandelt".
Bei Galileo wurde allerdings nichts anderes gemacht als das was ich gemacht habe. Jedenfalls steht nicht mehr im Openbook und ich habe die dazugehörige Projectdatei nicht um nachzuschauen.
Comment