148 lines
4.8 KiB
C#
148 lines
4.8 KiB
C#
namespace eccezioni_1;
|
|
|
|
class Program {
|
|
static void Main(string[] args) {
|
|
int scelta = 0;
|
|
int input = 0;
|
|
bool eccezione;
|
|
int[] array;
|
|
do {
|
|
|
|
|
|
Console.WriteLine("Scegliere un'opzione:");
|
|
Console.WriteLine("1. NullReferenceException array");
|
|
Console.WriteLine("2. NullReferenceExceptiopn stringa");
|
|
Console.WriteLine("3. DivideByZeroException");
|
|
Console.WriteLine("4. IndexOutOfBoundException");
|
|
Console.WriteLine("0. Esci");
|
|
Console.Write("Scelta: ");
|
|
|
|
eccezione = false;
|
|
try {
|
|
scelta = Convert.ToInt32(Console.ReadLine());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Opzione non valida.");
|
|
Pausa();
|
|
eccezione = true;
|
|
}
|
|
if (!eccezione) {
|
|
switch (scelta) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
try {
|
|
int[] T = null;
|
|
T[0] = 7;
|
|
}
|
|
catch (NullReferenceException e) {
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
break;
|
|
case 2:
|
|
try {
|
|
String s = null;
|
|
int l = s.Length;
|
|
}
|
|
catch (NullReferenceException e) {
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
break;
|
|
case 3:
|
|
do {
|
|
Console.Write("Inserire un numero: ");
|
|
eccezione = false;
|
|
try {
|
|
input = Convert.ToInt32(Console.ReadLine());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Numero non valido");
|
|
Pausa();
|
|
eccezione = true;
|
|
}
|
|
}
|
|
while (eccezione);
|
|
|
|
Console.WriteLine($"Il risultato della divisione è {Divisione(input)}");
|
|
break;
|
|
case 4:
|
|
array = Vettore();
|
|
for (int i = 0; i < array.Length; i++) {
|
|
Console.WriteLine($"Elemento n. {i}: {array[i]}");
|
|
}
|
|
Pausa();
|
|
break;
|
|
default:
|
|
Console.WriteLine("Opzione non valida.");
|
|
Pausa();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
while (eccezione || scelta != 0);
|
|
}
|
|
|
|
static void Pausa() {
|
|
Console.WriteLine("Premere un tasto per continuare. . .");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
static double Divisione(int input) {
|
|
Random r = new Random();
|
|
bool eccezione;
|
|
double ritorno;
|
|
do {
|
|
int numero = r.Next(0, 4);
|
|
eccezione = false;
|
|
try {
|
|
ritorno = input / numero;
|
|
}
|
|
catch (DivideByZeroException) {
|
|
Console.WriteLine("Impossibile dividere per 0.");
|
|
Pausa();
|
|
eccezione = true;
|
|
ritorno = double.NaN;
|
|
}
|
|
} while (eccezione);
|
|
return ritorno;
|
|
}
|
|
|
|
static int[] Vettore() {
|
|
int[] ritorno = new int[5];
|
|
int inputIndex = 0;
|
|
bool eccezione;
|
|
do {
|
|
Console.Write("In quale posizione inserire il numero? ");
|
|
eccezione = false;
|
|
try {
|
|
inputIndex = Convert.ToInt32(Console.ReadLine());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Numero non valido.");
|
|
Pausa();
|
|
eccezione = true;
|
|
}
|
|
if (!eccezione) {
|
|
do {
|
|
Console.Write("Inserire un numero: ");
|
|
eccezione = false;
|
|
try {
|
|
ritorno[inputIndex] = Convert.ToInt32(Console.ReadLine());
|
|
}
|
|
catch (FormatException) {
|
|
Console.WriteLine("Numero non valido");
|
|
Pausa();
|
|
eccezione = true;
|
|
}
|
|
catch (IndexOutOfRangeException e) {
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
while (eccezione);
|
|
}
|
|
}
|
|
while (eccezione);
|
|
return ritorno;
|
|
}
|
|
}
|