Google Translate.Widget by : Blog Tutorial
ArabicKoreanJapaneseChinese SimplifiedEnglishFrenchGermanSpainItalianDutchRussianPortuguese


VB6 Error Handling

When we make a program with VB6, we will write the program code in a procedure or function. This procedure which will be run in an event such as when the form loads, click the button, the form resize etc.. If there is an error code in the procedure or function that we make, then the program will error and stop the program
Well, in this post I'll try to explain about controling of errors so that if there is a fault code, the program will run the command as we want.

To do this, there are several methods as follows:
1. on error resume next
with this command, the program will ignore the error code that occurred and will continue to the next line of code. Example:

private sub Form_Load()
On Error Resume Next
x = 20
y = 0
z = x/y
MsgBox z
end sub

When the procedure is not given the method of "on error resume next", it will generate an error because the division of the denominator can not be 0.


2. on error goto "Labels"
This command serves as a trap errors. The point is that every time there is an error code, the code will be transferred to the label, which label may include the commands according to our wishes. Lines of code after the error will not be executed. Example:
Private Sub Form_Load()
On Error GoTo ErrHand
x = 20
y = 0
z = x/y
MsgBox z
Exit Sub
ErrHand:
MsgBox "Terjadi kesalahan"
End Sub

When the procedure is executed, then every time something goes wrong, the program immediately run the command MsgBox "Terjadi kesalahan", and the procedure will stop here.

3. catch errors, but still run the correct code
So how if we want only the wrong code are not executed, but the correct code still runs. Consider the following example :
Private Sub Form_Load()
On Error Resume Next
x = 20
y = 0
i = 5
z = x/y
n = x / i
MsgBox z
MsgBox n
If Err.Number Then
MsgBox "Terjadi kesalahan" & Err.Description
Else
End If
End Sub

From this example, the line of code z = x / y are not going to run, but n = x / i still run because there are no mistakes on that line.

That's a little info from me, may be useful.

7 komentar:

alamendah said...

(maaf) izin mengamankan PERTAMA dulu. Boleh kan?!
Sori kali ini saya benar2 belum paham.
Biar tak bacanya lagi pelan, OK?

Pencerah said...

Sayang, dah lupa ama yg namanya vb

devxecutor said...

@alamendah : ok bro..Silahkan..PERTAMA terus ya hehe..
@Pencerah : wow..dah migrasi ke java ato lain ya bro?Tapi vb6 masih ok juga koq.. :D

Digital Life said...

Very nice brother..

marsudiyanto said...

Aku yo bisa VB tapi sebatas tak nggo dewe, ora pati maniac

devxecutor said...

@pak Mars : aku malah ra pati nggatekke hehe..

Hitler Jr. said...

Dulu pernah belajar VB tapi binggung akhirnya langsung gak kuat....
Ajarin saya yach sob