62 lines
2.1 KiB
C#
62 lines
2.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
|
|
namespace Exercises_C_Sharp.E32_Exceptions
|
|
{
|
|
class Exercise_3
|
|
{
|
|
public static void Start()
|
|
{
|
|
//Rufen Sie die Methode ThrowException so lange auf, bis alle Exceptions geflogen und auf der Konsole ausgegeben wurden:
|
|
//Code Start
|
|
bool[] bools = new bool[5];
|
|
|
|
while(true)
|
|
{
|
|
try {ThrowException();}
|
|
catch(FileNotFoundException){bools[0] = true;}
|
|
catch(EntryPointNotFoundException){bools[1] = true;}
|
|
catch(FieldAccessException){bools[2] = true;}
|
|
catch(HttpRequestException){bools[3] = true;}
|
|
catch(Exception){bools[4] = true;}
|
|
bool temp = false;
|
|
foreach(var element in bools)
|
|
if(!element) temp = true;
|
|
if(!temp) break;
|
|
|
|
//ODER:
|
|
int number = 0;
|
|
try {ThrowException();}
|
|
catch(FileNotFoundException){number |= 1;}
|
|
catch(EntryPointNotFoundException){number |= 2;}
|
|
catch(FieldAccessException){number |= 4;}
|
|
catch(HttpRequestException){number |= 8;}
|
|
catch(Exception){number |= 16;}
|
|
|
|
if(number == 31) break;
|
|
}
|
|
|
|
//Code ENDE
|
|
}
|
|
|
|
static void ThrowException()
|
|
{
|
|
Random rand = new Random();
|
|
switch(rand.Next(0,5))
|
|
{
|
|
case 0:
|
|
throw new FileNotFoundException("Die Datei konnte nicht gefunden werden!");
|
|
case 1:
|
|
throw new EntryPointNotFoundException("Leider konnte der Beginn nicht identifiziert werden.");
|
|
case 2:
|
|
throw new FieldAccessException("Es kann nicht auf die Resource zugegriffen werden.");
|
|
case 3:
|
|
throw new HttpRequestException("Der Request konnte nicht verarbeitet werden.");
|
|
case 4:
|
|
throw new Exception("Es ist ein unbekannter Fehler aufgetreten...");
|
|
}
|
|
}
|
|
|
|
}
|
|
} |