|
-
Jul 6th, 2000, 03:44 PM
#1
Thread Starter
Fanatic Member
Hey,
can someone give me the simplest example of error trapping?
if there is no simple example, no prob.
thanks
-
Jul 6th, 2000, 03:48 PM
#2
Hyperactive Member
Code:
Function uh() As Boolean
Dim i as Integer
On Error Goto Oops
i = 'a'
uh = true
exit function
Oops:
uh = false
End Function
-
Jul 6th, 2000, 04:03 PM
#3
This example will create an Overflow and display a
MessageBox telling us what happened.
Code:
'The VB to go to ERR_HANDLE if an Error occurs
On Error GoTo ERR_HANDLE
'Create an Overflow (Error 6)
Err.Raise 6
Exit Sub
ERR_HANDLE:
'Display a MessageBox telling us we have an Error
If Err = 6 Then MsgBox ("Over flow, click to resume")
-
Jul 6th, 2000, 04:39 PM
#4
Thread Starter
Fanatic Member
why?
I am sure there is a good reason to do this but I am not yet sure why. When my programs have problems they automaticly tell me.
but mine are not .exe so maybe that is why.
um...don't know.
I am geussing this is a way to track errors in a program.
so would you say something like
if err=6 then
if err=4 then
if err=3 then....ect.
and list every error possible?
I am new to this can you tell?
thanks
-
Jul 6th, 2000, 04:56 PM
#5
If you want to some a specific action when a specific error occurs, yes, you can use the above statements. Use the On Error Resume Next statement to skip the statement where the error occured and continue execution of the program.
Code:
'If an Error occurs, skip that statement
'continue execution of the program
On Error Resume Next
'Create an Overflow (Error 6)
Err.Raise 6
Print "Hello"
-
Jul 6th, 2000, 11:33 PM
#6
Hyperactive Member
you can also use "Err.Description" to display the error message.
code:
On Error GoTo ErrMsg
'your code
ErrMsg:
Msgbox Err.Description
-
Jul 7th, 2000, 02:20 AM
#7
Hyperactive Member
pnj:
If you run a program from within VB it depends on the settings when and how an error is shown.
But in exe's, it doesn't work that way....
The other examples show how to trap an error.
If you know you can expect certain errors (for instance, file does not exist, file is locked, etc), you can add to you errorhandler:
If Err.Number = <certain number> Then
' action here
ElseIf Err.Number = <other number> Then
' do something else
Else
' display an error, shutdown windows, whatever
End If
That way you can do specific task when certain errors occur.
-
Jul 7th, 2000, 07:29 AM
#8
transcendental analytic
Error trapping is the weak programmers easy way out.
Ok, that was maybe not what i meant but i thought it sounded very familiar in some way. Well but that's somehow became my motto
Often you can avoid error handling by just using if statement to prevent the error from occuring,
I.E:
but sometimes you have to deal with it, whenever you don't know how to prevent it:
Code:
On error resume next
c=b/a
if err=11 then err.clear:msgbox "Division by 0"
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 7th, 2000, 03:36 PM
#9
Frenzied Member
I'd Dissagree with that ked.
I use error trapping if I'm using the same function in a huge loop where in some cases the parameters will raise an error. For example if I'm making an ActiveX control that paints itself to look like a shere, the way I do this is to go through every pixel in my grid, find out it's distance from the centre and then find the colour of that picel based on
Code:
Sqr((usercontrol.Width * usercontrol.Width * 4) - (R * R))
This will raise an error if its radius is greater than usercontrol.width/2 it's quicker to set up an error trap and just skip some code if this raises an error than it would be to check it myself, especially if it's a large activeX control with lots of pixels to calculate.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|