Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Haaste: Virheenkäsittely | Tietorakenteet ja Tiedostojen Käsittely
Quizzes & Challenges
Quizzes
Challenges
/
C# Perusteiden Jälkeen

bookHaaste: Virheenkäsittely

try-catch-rakenteessa on lisäsyntaksi, jonka avulla voidaan käsitellä tietyn tyyppisiä virheitä erikseen:

index.cs

index.cs

copy
1234567891011121314151617
try { // 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ä:

  1. DivideByZeroException: jakolasku nollalla;
  2. FileNotFoundException: tiedostoa, johon yritetään päästä käsiksi, ei ole olemassa;
  3. KeyNotFoundException: sanakirjan avainta ei ole olemassa;
  4. 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

index.cs

copy
12345678910111213
try { // code } catch (ExceptionType) { // code } ... finally { // code }

Tässä on esimerkki tällaisen try-catch-lohkon käytöstä:

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using 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

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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"); } } } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 10

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookHaaste: 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

index.cs

copy
1234567891011121314151617
try { // 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ä:

  1. DivideByZeroException: jakolasku nollalla;
  2. FileNotFoundException: tiedostoa, johon yritetään päästä käsiksi, ei ole olemassa;
  3. KeyNotFoundException: sanakirjan avainta ei ole olemassa;
  4. 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

index.cs

copy
12345678910111213
try { // code } catch (ExceptionType) { // code } ... finally { // code }

Tässä on esimerkki tällaisen try-catch-lohkon käytöstä:

index.cs

index.cs

copy
123456789101112131415161718192021222324252627
using 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

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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

index.cs

copy
1234567891011121314151617181920212223242526272829303132333435
using 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"); } } } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 10
some-alt