Sfida: Gestione degli Errori
La sintassi try-catch
dispone di una sintassi aggiuntiva che consente di intercettare tipi specifici di errori e gestirli separatamente:
index.cs
1234567891011121314151617try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }
Il tipo Exception
utilizzato nel capitolo precedente intercetta semplicemente tutti i tipi di errori; tuttavia, esistono alcuni sottotipi di Exception
che intercettano errori più specifici. Di seguito sono riportati alcuni tipi comuni di eccezioni:
DivideByZeroException
: si verifica una divisione per zero;FileNotFoundException
: il file a cui si sta accedendo non esiste;KeyNotFoundException
: la chiave del dizionario non esiste;IndexOutOfRangeException
: l'indice specificato di un array o di una lista non è valido.
Il termine errorVarName
è una variabile che memorizza l'oggetto Exception e contiene informazioni come il messaggio di errore, accessibile tramite errorVarName.Message
. È possibile ometterlo nel caso in cui non venga utilizzato:
index.cs
12345678910111213try { // code } catch (ExceptionType) { // code } ... finally { // code }
Ecco un esempio di utilizzo di questo tipo di blocco try-catch
:
index.cs
123456789101112131415161718192021222324252627using System; class Program { static void Main(string[] args) { int[] myArray = { 0, 2, 4, 6, 8, 10 }; int i = 0; while (true) { try { Console.Write(myArray[i] / i + " "); i++; } catch(DivideByZeroException) { i++; } catch(IndexOutOfRangeException) { break; } } } }
Ora, utilizzando questi concetti. Completa gli spazi vuoti con i tipi di eccezione pertinenti nel seguente codice per completare la sfida.
index.cs
1234567891011121314151617181920212223242526272829303132333435using System; using System.Collections.Generic; class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 5, 7, 9 }; var numberNames = new Dictionary<int, string>(); numberNames.Add(1, "One"); numberNames.Add(2, "Two"); numberNames.Add(5, "Five"); numberNames.Add(9, "Nine"); int i = 0; while (true) { try { int key = numbers[i]; Console.WriteLine($"{key} is {numberNames[key]}"); i++; } catch (___) { break; } catch (___) { numberNames.Add(7, "Seven"); } } } }
Utilizza il tipo di eccezione pertinente per ciascun blocco catch
. Leggi il codice e comprendi quale blocco catch
è più appropriato per gestire un determinato tipo di eccezione.
index.cs
1234567891011121314151617181920212223242526272829303132333435using System; using System.Collections.Generic; public class HelloWorld { public static void Main(string[] args) { int[] numbers = { 1, 2, 5, 7, 9 }; var numberNames = new Dictionary<int, string>(); numberNames.Add(1, "One"); numberNames.Add(2, "Two"); numberNames.Add(5, "Five"); numberNames.Add(9, "Nine"); int i = 0; while (true) { try { int key = numbers[i]; Console.WriteLine($"{key} is {numberNames[key]}"); i++; } catch (IndexOutOfRangeException) { break; } catch (KeyNotFoundException) { numberNames.Add(7, "Seven"); } } } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
What are the correct exception types to fill in the blanks?
Can you explain why each exception type is used in this context?
Can you give more examples of specific exception types?
Awesome!
Completion rate improved to 2.04
Sfida: Gestione degli Errori
Scorri per mostrare il menu
La sintassi try-catch
dispone di una sintassi aggiuntiva che consente di intercettare tipi specifici di errori e gestirli separatamente:
index.cs
1234567891011121314151617try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }
Il tipo Exception
utilizzato nel capitolo precedente intercetta semplicemente tutti i tipi di errori; tuttavia, esistono alcuni sottotipi di Exception
che intercettano errori più specifici. Di seguito sono riportati alcuni tipi comuni di eccezioni:
DivideByZeroException
: si verifica una divisione per zero;FileNotFoundException
: il file a cui si sta accedendo non esiste;KeyNotFoundException
: la chiave del dizionario non esiste;IndexOutOfRangeException
: l'indice specificato di un array o di una lista non è valido.
Il termine errorVarName
è una variabile che memorizza l'oggetto Exception e contiene informazioni come il messaggio di errore, accessibile tramite errorVarName.Message
. È possibile ometterlo nel caso in cui non venga utilizzato:
index.cs
12345678910111213try { // code } catch (ExceptionType) { // code } ... finally { // code }
Ecco un esempio di utilizzo di questo tipo di blocco try-catch
:
index.cs
123456789101112131415161718192021222324252627using System; class Program { static void Main(string[] args) { int[] myArray = { 0, 2, 4, 6, 8, 10 }; int i = 0; while (true) { try { Console.Write(myArray[i] / i + " "); i++; } catch(DivideByZeroException) { i++; } catch(IndexOutOfRangeException) { break; } } } }
Ora, utilizzando questi concetti. Completa gli spazi vuoti con i tipi di eccezione pertinenti nel seguente codice per completare la sfida.
index.cs
1234567891011121314151617181920212223242526272829303132333435using System; using System.Collections.Generic; class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 5, 7, 9 }; var numberNames = new Dictionary<int, string>(); numberNames.Add(1, "One"); numberNames.Add(2, "Two"); numberNames.Add(5, "Five"); numberNames.Add(9, "Nine"); int i = 0; while (true) { try { int key = numbers[i]; Console.WriteLine($"{key} is {numberNames[key]}"); i++; } catch (___) { break; } catch (___) { numberNames.Add(7, "Seven"); } } } }
Utilizza il tipo di eccezione pertinente per ciascun blocco catch
. Leggi il codice e comprendi quale blocco catch
è più appropriato per gestire un determinato tipo di eccezione.
index.cs
1234567891011121314151617181920212223242526272829303132333435using System; using System.Collections.Generic; public class HelloWorld { public static void Main(string[] args) { int[] numbers = { 1, 2, 5, 7, 9 }; var numberNames = new Dictionary<int, string>(); numberNames.Add(1, "One"); numberNames.Add(2, "Two"); numberNames.Add(5, "Five"); numberNames.Add(9, "Nine"); int i = 0; while (true) { try { int key = numbers[i]; Console.WriteLine($"{key} is {numberNames[key]}"); i++; } catch (IndexOutOfRangeException) { break; } catch (KeyNotFoundException) { numberNames.Add(7, "Seven"); } } } }
Grazie per i tuoi commenti!