EXCEPTION HANDLING
Disusun oleh :
Try
[ tryStatements ]
[ Exit Try ]
[ Catch [ exception [ As type ] ] [ When expression ]
[ catchStatements ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finallyStatements ] ]
End Try
Module exceptionProg
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch e As DivideByZeroException
Console.WriteLine("Exception tertangkap: {0}", e)
Finally
Console.WriteLine("Hasil: {0}", result)
End Try End
Sub Sub Main()
division(25, 0)
Console.ReadKey()
End Sub
End Module
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
Module exceptionProg
Public Class TempIsZeroException : Inherits ApplicationException
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
Public Class Temperature
Dim temperature As Integer = 0
Sub showTemp()
If (temperature = 0) Then
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut :
TempIsZeroException: Zero Temperature found
d. Objek Throw
Anda dapat melemparkan sebuah benda apakah itu baik secara langsung maupun tidak
langsung berasal dari kelas System.Exception.
Anda dapat menggunakan pernyataan throw di blok catch untuk membuang objek
hadir sebagai :
Throw [ expression ]
Module exceptionProg
Sub Main()
Try
Throw New ApplicationException("A custom exception _
is being thrown here...")
Catch e As Exception
Console.WriteLine(e.Message)
Finally
Console.WriteLine("Now inside the Finally Block")
End Try
Console.ReadKey()
End Sub
End Module
Contoh :
On Error Resume Next Dim A As Integer
X = TextBox1.Text
TextBox1.Text=”String”
MsgBox(ex.ToString)
MsgBox(“Abaikan Kesalahan”)
f. On Error Goto
Merupakan perintah penanganan kesalahan apabila terjadi kesalahan terhadap suatu baris
tertentu maka perintah akan melompat ke baris yang dituju akan dijalankan.
Contoh :
MsgBox(“Abaikan
Kesalahan”)
Selanjutnya tambahkan kontrol ke form seperti pada gambar diatas dan atur
propertinya seperti tabel berikut :
Case "<>"
TxtHasil.Text = TxtNilai1.Text <>
TxtNilai2.Text
Case "<"
TxtHasil.Text = TxtNilai1.Text <
TxtNilai2.Text
Case ">"
TxtHasil.Text = TxtNilai1.Text >
TxtNilai2.Text
Case "<="
TxtHasil.Text = TxtNilai1.Text <=
End Try
End Sub
End Class
JAWAB :
KODE PROGRAM :
Selanjutnya tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya
seperti tabel berikut :
Imports System.Windows.Forms
Sub aktif()
RadioButton1.Enabled = True
RadioButton2.Enabled = True
RadioButton3.Enabled = True
ListBox1.Enabled = True
ComboBox1.Enabled = True
TextBox1.Enabled = True
Sub total()
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
End Sub
Sub kembali()
Try
If TextBox4.Text >= TextBox3.Text Then
TextBox5.Text = Val(TextBox4.Text) -
Val(TextBox3.Text)
ElseIf TextBox4.Text < TextBox3.Text Then
MessageBox.Show("Uang Anda Kurang", "Peringatan
!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Catch ex As Exception
MsgBox("Data yang anda masukkan salah", "Peringatan !")
End Try
End Sub
Sub batal()
ListBox1.Items.Clear()
ComboBox1.Text = "MejaNo"
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
RadioButton1.Enabled = False
RadioButton2.Enabled = False
RadioButton3.Enabled = False
ListBox1.Enabled = False
ComboBox1.Enabled = False
TextBox1.Enabled = False
TextBox2.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox5.Enabled = False
Button2.Enabled = False
Button1.Enabled = False
End Sub
Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti
tabel berikut:
JAWAB :
KODE PROGRAM :
V. RANGKUMAN
Exception Handling adalah perintah ini digunakan untuk menangani kesalahan
dalam menjalankan aplikasi, dengan adanya perintah ini setiap kesalahan akan diatasi
secara otomatis sesuai dengan perintah penanganan kesalahan yang telah dibuat, sehingga
tidak terjadi kemacetan aplikasi yang sedang berjalan.
Struktur Exception Handling atau penanganan kesalahan pada visual basic 2008
antara lain :