Anda bisa daftar ke beberapa pernyataan untuk menangkap berbagai jenis pengecualian dalam kasus blok try,
Anda meningkatkan lebih dari satu pengecualian dalam situasi yang berbeda.
Kelas System.ApplicationException mendukung exception yang dihasilkan oleh program aplikasi. Jadi
exception didefinisikan oleh programmer harus berasal dari kelas ini.
Kelas System.SystemException adalah kelas dasar untuk semua sistem kecuali yang telah ditetapkan.
Tabel berikut memberikan beberapa kelas pengecualian yang telah ditetapkan berasal dari kelas
Sytem.SystemException:
Sub Main()
Dim temp As Temperature = New Temperature()
Try
temp.showTemp()
Catch e As TempIsZeroException
Console.WriteLine("TempIsZeroException: {0}", e.Message)
End Try
Console.ReadKey()
End Sub
End Module
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
TempIsZeroException: Zero Temperature found
Ketika kode di atas dikompilasi dan dijalankan, menghasilkan hasil sebagai berikut:
A custom exception is being thrown here...
Now inside the Finally Block
Selanjutnya tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti
tabel berikut:
Object Properties Nilai
Form3 Name Form3
Text Operator
Label1 Text Nilai1
Label2 Text Operator
Label3 Text Nilai2
Label4 Text Hasil
TextBox1 Name TxtNilai1
ComboBox1 Name CmbOperator
TextBox2 Name TxtNilai2
TextBox3 Name TxtHasil
Button1 Name BtnProses
Text Proses
End Try
End Sub
End Class
a) Jalankan aplikasi dengan menekan tombol F5 (di keyboard), atau melalui icon Start Debugging di
toolbar, atau melalui menu Debug > Start Debuging.
Hasil Program :
2) Buatlah program untuk penanganan expsepsi dengan desain tampilan sebagai berikut :
Selanjutnya tambahkan kontrol ke form seperti pada gambar diatas dan atur
propertinya seperti tabel berikut:
Object Properties Nilai
Form1 Name Form1
Text Form1
Groupbox Text Menu
RadioButton1 Text Asia
RadioButton2 Text America
RadioButton3 Text Eropa
ListBox1
Label1 Text Harga
Label2 Text Banyak Pesan
Label3 Text Total
Label4 Text Bayar
Label5 Text Kembali
Label6 Text No. Meja
TexBox1 Name TextBox1
TexBox2 Name TextBox2
TexBox3 Name TextBox3
Enable False
TexBox4 Name TextBox4
TexBox5 Name TextBox5
Enable False
ComboBox1
Button1 Name Button1
Text Pesan
Button2 Name Button2
Text Reject
Button3 Name Button3
Text Selesai
Sub aktif()
RadioButton1.Enabled = True
RadioButton2.Enabled = True
RadioButton3.Enabled = True
ListBox1.Enabled = True
ComboBox1.Enabled = True
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = False
TextBox4.Enabled = True
TextBox5.Enabled = False
Button1.Enabled = True
Button3.Enabled = True
End Sub
Sub total()
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
End Sub
Sub kembali()
Try
Dim x As Integer = Integer.Parse(TextBox2.Text)
Dim y As Integer = Integer.Parse(TextBox4.Text)
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
b) Jalankan aplikasi dengan menekan tombol F5 (di keyboard), atau melalui icon Start
Debugging di toolbar, atau melalui menu Debug > Start Debuging.
Buatlah program untuk penanganan expsepsi dengan desain tampilan sebagai berikut :
Selanjutnya tambahkan kontrol ke form seperti pada gambar diatas dan atur
propertinya seperti tabel berikut:
Object Properties Nilai
Form1 Name Form1
Text Form1
Groupbox Text Menu
RadioButton1 Text Asia
RadioButton2 Text America
RadioButton3 Text Eropa
ListBox1
Label1 Text Harga
Label2 Text Banyak Pesan
Label3 Text Total
Label4 Text Bayar
Label5 Text Kembali
Label6 Text No. Meja
TexBox1 Name TextBox1
TexBox2 Name TextBox2
TexBox3 Name TextBox3
Enable False
TexBox4 Name TextBox4
TexBox5 Name TextBox5
Enable False
ComboBox1
Button1 Name Button1
Text Pesan
Button2 Name Button2
Text Reject
Button3 Name Button3
Text Selesai
Sub aktif()
RadioButton1.Enabled = True
RadioButton2.Enabled = True
RadioButton3.Enabled = True
ListBox1.Enabled = True
ComboBox1.Enabled = True
TextBox1.Enabled = True
TextBox2.Enabled = True
TextBox3.Enabled = False
TextBox4.Enabled = True
TextBox5.Enabled = False
Button1.Enabled = True
Button3.Enabled = True
End Sub
Sub total()
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
End Sub
Sub kembali()
Try
Dim x As Integer = Integer.Parse(TextBox2.Text)
Dim y As Integer = Integer.Parse(TextBox4.Text)
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
d) Jalankan aplikasi dengan menekan tombol F5 (di keyboard), atau melalui icon Start
Debugging di toolbar, atau melalui menu Debug > Start Debuging.
HASIL PROGRAM :
11.5 Soal Latihan
1) Buatlah desain Tampilan form seperti gambar berikut ini :
Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya
seperti tabel berikut:
Object Properties Nilai
Form1 Name Form1
Text Converter
TexBox1 Name TxtRupiah
TexBox2 Name TxtDollar
Button1 Name BtnConver
Text Proses
Kode Program :
Hasil Program :