Kirjoittaja: tnb
Kirjoitettu: 24.04.2004 – 24.04.2004
Tagit: koodi näytille, vinkki
Vb.net tuo tullessaan rakenteellisen virheenkäsittelyn, joka selkeyttää ja helpottaa poikeamien (exceptions) käsittelyä.
Listaus 1 näyttää esimerkin virheenkäsittelyn perustapauksesta.
Listaus 2 on esimerkki eritellystä poikkeamankäsittelystä.
Listauksessa 3 on esitetty miten tehdään oma poikeaman-luokka ja listauksessa 4 on esimerkki, miten omaa poikkeamankäsittelyluokkaa käytetään.
Projektissa on form1 jossa buttonit 1..3
sekä form2 jossa button1.
form1, button1
'Perusrakenne virheenkäsittelyyn
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Try
'tässä välissä on koodi, jossa voi sattua virhe eli poikkeama
a = 1 \ a ' jako nollalla
Catch ex As Exception
'tämä tehdään vain jos poikkeama
MsgBox(ex.Message)
Finally
'tämä tehdään aina
MsgBox("Jakolaskun tulos= " & a.ToString)
End Try
End Subform1, button2
' poikeamien erittely koodin sisällä
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer = 0
Dim c As Integer = 1
Dim b(10) As Integer
Dim strP As String = "QWERTY"
Try
'valitse näistä vain yksivirhe
'c = strP ' väärä muuttujan tyyppi
'c = c + 1 : a = c \ a ' oma sääntö:kakkonen ei salittu jakaa nollalla
'a = c \ a ' jako nollalla virhe
'b(11) = 9 ' indeksi liian suuri
c = Math.Sqrt(-1) ' ei pysty laskemaan tätä
Catch ex As Exception When c = 2
MsgBox(ex.Message, , "Kakkosen jako nollalla on raskauttavaa")
Catch ex As DivideByZeroException
MsgBox(ex.Message, , "Jako nollalla")
Catch ex As ArithmeticException
MsgBox(ex.Message, , "Laskenta ei onnistu")
Catch ex As IndexOutOfRangeException
MsgBox(ex.Message, , "Taulukon indeksi turhan iso")
Catch ex As InvalidCastException
MsgBox(ex.Message, , "Väärä muuttujan tyyppi")
Catch ex As Exception ' loput tyypit
MsgBox(ex.Message)
End Try
End Sub
' form1, button3
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' näyttää formin 2
Dim uusiFormi As New Form2
uusiFormi.Show()
End Submoduuli1
Module Poikkeusmoduli
Public Class PoikkeamanKäsittely
Inherits System.ApplicationException
Private _syy As String
' kolme tapaa muodostaa objekti
Public Sub New() 'ei parametrejä
End Sub
Public Sub New(ByVal Viesti As String) ' oma viesti kyytiin
MyBase.New(Viesti)
_syy = Viesti
End Sub
Public Sub New(ByVal Viesti As String, ByVal SisäinenPoikkeama As Exception)
MyBase.New(Viesti, SisäinenPoikkeama)
Dim oTyyppiVirhe As New InvalidCastException
Dim oYlivuotoVirhe As New OverflowException
Dim oLaskentaVirhe As New ArithmeticException
Dim oIndexiVirhe As New IndexOutOfRangeException
Dim oJakoNollallaVirhe As New DivideByZeroException
_syy = SisäinenPoikkeama.Message
If SisäinenPoikkeama.GetType Is oTyyppiVirhe.GetType Then _syy = "Käytetty väärän tyyppistä muuttujaa"
If SisäinenPoikkeama.GetType Is oLaskentaVirhe.GetType Then _syy = "Laskentaa ei voi suorittaa"
If SisäinenPoikkeama.GetType Is oYlivuotoVirhe.GetType Then _syy = "Ylivuoto, laskentaa ei voi suorittaa"
If SisäinenPoikkeama.GetType Is oIndexiVirhe.GetType Then _syy = "Indeksi turhan iso"
If SisäinenPoikkeama.GetType Is oJakoNollallaVirhe.GetType Then _syy = "Jako nollalla"
End Sub
Public Property Syy()
Get
Syy = _syy
End Get
Set(ByVal Value)
_syy = Syy
End Set
End Property
End Class
End Moduleform2, button1
'esimerkki oman poikkeamankäsittelyn soveltamisesta
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim c As Integer = 1
Dim b(10) As Integer
Dim strP As String = "QWERTY"
Try
'valitse näistä vain yksivirhe
'c = strP ' väärä muuttujan tyyppi
'a = c \ a ' jako nollalla virhe
'b(11) = 9 ' indeksi liian suuri
c = Math.Sqrt(-1) ' ei pysty laskemaan tätä, ylivuoto
Catch ex As Exception
Dim omaPoikkeama As New PoikkeamanKäsittely("Virhe ohjelmassa", ex)
MsgBox(omaPoikkeama.Syy, , omaPoikkeama.Message)
'Throw omaPoikkeama ' jos halutaan antaa virhe eteenpäin esim kehitysvaiheessa
End Try
End Sub
End ClassTästä on varmasti hyötyä monelle vb.Net:n alkeita opettelevalle!
Tuo, että pistät modulen sisään tuon luokkasi on harvinaisen turhaa, koska module on vb6 peruja ja käännetään luokaksi joka tapauksessa. Public muuttujien yms tekeminen saavutetaan Shared -avainsanalla.