Haaste: Virheenkäsittely
try-catch-rakenteessa on lisäsyntaksi, jonka avulla voidaan käsitellä tietyn tyyppisiä virheitä erikseen:
index.cs
1234567891011121314151617try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }
Edellisessä luvussa käytetty tyyppi Exception sieppaa kaikki virhetyypit, mutta on olemassa myös tarkempia Exception-alatyyppejä, jotka käsittelevät tiettyjä virheitä. Alla on joitakin yleisiä poikkeustyyppejä:
DivideByZeroException: jakolasku nollalla;FileNotFoundException: tiedostoa, johon yritetään päästä käsiksi, ei ole olemassa;KeyNotFoundException: sanakirjan avainta ei ole olemassa;IndexOutOfRangeException: taulukon tai listan indeksi on virheellinen.
Termi errorVarName on muuttuja, joka tallentaa Exception-olion ja sisältää tietoa, kuten virheilmoituksen, johon pääsee käsiksi errorVarName.Message-ominaisuuden kautta. Tämän voi jättää pois, jos sitä ei käytetä:
index.cs
12345678910111213try { // code } catch (ExceptionType) { // code } ... finally { // code }
Tässä on esimerkki tällaisen try-catch-lohkon käytöstä:
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; } } } }
Nyt käyttäen näitä käsitteitä. Täydennä alla oleva koodi sopivilla poikkeustyypeillä haasteen suorittamiseksi.
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"); } } } }
Käytä kuhunkin catch-lohkoon sopivaa poikkeustyyppiä. Lue koodi ja ymmärrä, mikä catch-lohko soveltuu parhaiten tietyn poikkeuksen käsittelyyn.
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"); } } } }
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 2.04
Haaste: Virheenkäsittely
Pyyhkäise näyttääksesi valikon
try-catch-rakenteessa on lisäsyntaksi, jonka avulla voidaan käsitellä tietyn tyyppisiä virheitä erikseen:
index.cs
1234567891011121314151617try { // code } catch (ExceptionType errorVarName) { // code } catch (ExceptionType errorVarName) { // code } ... finally { // code }
Edellisessä luvussa käytetty tyyppi Exception sieppaa kaikki virhetyypit, mutta on olemassa myös tarkempia Exception-alatyyppejä, jotka käsittelevät tiettyjä virheitä. Alla on joitakin yleisiä poikkeustyyppejä:
DivideByZeroException: jakolasku nollalla;FileNotFoundException: tiedostoa, johon yritetään päästä käsiksi, ei ole olemassa;KeyNotFoundException: sanakirjan avainta ei ole olemassa;IndexOutOfRangeException: taulukon tai listan indeksi on virheellinen.
Termi errorVarName on muuttuja, joka tallentaa Exception-olion ja sisältää tietoa, kuten virheilmoituksen, johon pääsee käsiksi errorVarName.Message-ominaisuuden kautta. Tämän voi jättää pois, jos sitä ei käytetä:
index.cs
12345678910111213try { // code } catch (ExceptionType) { // code } ... finally { // code }
Tässä on esimerkki tällaisen try-catch-lohkon käytöstä:
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; } } } }
Nyt käyttäen näitä käsitteitä. Täydennä alla oleva koodi sopivilla poikkeustyypeillä haasteen suorittamiseksi.
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"); } } } }
Käytä kuhunkin catch-lohkoon sopivaa poikkeustyyppiä. Lue koodi ja ymmärrä, mikä catch-lohko soveltuu parhaiten tietyn poikkeuksen käsittelyyn.
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"); } } } }
Kiitos palautteestasi!