Skip to content
Snippets Groups Projects
Commit 4c5cfaed authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

2020-05-26b

parent 90e4489d
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -8,12 +8,16 @@ namespace _06Exceptions
{
class Program
{
class Quadrierfehler : ArgumentOutOfRangeException
class MyAppExceptions : Exception
{
public Quadrierfehler(string Fehlertext) : base(Fehlertext) { }
public MyAppExceptions(string Fehlertext="") : base(Fehlertext) { }
}
class MyInputExceptions : MyAppExceptions
{ }
static int funktion2(int x)
{
//MyAppExceptions ex = new MyAppExceptions("Fehlertest");
//throw ex;
if (x < 0)
throw new ArgumentOutOfRangeException("Fehler in funktion2");
return x * x * x;
......@@ -22,7 +26,7 @@ namespace _06Exceptions
{
funktion2(x);
if (x < 0)
throw new Quadrierfehler("So nicht!!!");
throw new MyAppExceptions("So nicht!!!");
if (x == 0)
throw new ArgumentOutOfRangeException("Argument darf nicht null sein");
return x * x;
......@@ -35,7 +39,7 @@ namespace _06Exceptions
funktion(-3);
funktion(-2);
}
catch (Quadrierfehler q)
catch (MyAppExceptions q)
{
Console.WriteLine("Quadrierfehler");
}
......
......@@ -9,26 +9,71 @@ namespace _06_UebgDiExceptions
// und Funktion2 ruft Funktion3. Setzen Sie hinter die catches jeweils ein finally
// Schreiben Sie entsprechende catch-Methoden, werfen Sie den Fehler weiter
// und stellen Sie sicher, dass das Programm unter keinen Umständen mit einer Exception beendet wird!
class MyAppExceptions : Exception
{
public int fehlerwert;
public MyAppExceptions(string Fehlertext, int fehlerwert) : base(Fehlertext)
{
this.fehlerwert = fehlerwert;
}
}
class Program
{
static void Funktion2()
static void Funktion1()
{
Console.WriteLine("Funktion1");
try
{
Funktion2();
Console.WriteLine("Letzte Anweisung des try-Blocks von Funktion 1");
}
catch (ArgumentException)
{ }
finally
{
Console.WriteLine("Finally von Funktion 1");
}
Console.WriteLine("Ende von Funktion1");
}
static void Funktion2()
{
Console.WriteLine("Funktion2");
try
{
Funktion3();
Console.WriteLine("Letzte Anweisung des try-Blocks von Funktion 2");
}
catch()
catch(MyAppExceptions e)
{
Console.WriteLine($"catch in Funktion 2: {e.Message}, {e.fehlerwert}");
throw;
}
finally
{
Console.WriteLine("Finally von Funktion 2");
}
Console.WriteLine("Ende von Funktion2");
}
static void Funktion3()
{
Console.WriteLine("Funktion3");
int x = 2;
if (1 < x)
throw new MyAppExceptions("Fehler in Funktion3", 4711);
Console.WriteLine("Ende von Funktion3");
}
static int errno=0;
static void Funktion3b()
{
int x = 0;
errno = 9999;
int y = 3 / x;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Funktion1();
}
}
}
No preview for this file type
No preview for this file type
......@@ -10,3 +10,4 @@ C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\06 UebgDiExceptions\obj\Debug
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\06 UebgDiExceptions\obj\Debug\netcoreapp3.1\06 UebgDiExceptions.dll
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\06 UebgDiExceptions\obj\Debug\netcoreapp3.1\06 UebgDiExceptions.pdb
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\06 UebgDiExceptions\obj\Debug\netcoreapp3.1\06 UebgDiExceptions.genruntimeconfig.cache
C:\Users\wienkop\source\repos\prog2-ss2020-wienkop\06 UebgDiExceptions\obj\Debug\netcoreapp3.1\06 UebgDiExceptions.csprojAssemblyReference.cache
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment