Error handling (1 Viewer)

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 23:59
Joined
Apr 27, 2015
Messages
6,337
This is a plug for one of our prolific members, pbaldy. Not too long ago I was having an issue with my error handling code and Paul shared a link with me that made the light bulb go bright:

http://www.baldyweb.com/ErrorTrap.htm

It is from his web site and it is very basic...but...once you grasp the concept, it really makes your applications run much smoother. My code runs much more efficiently and allows me to clean up after myself and not leave my users in a Dazed and Confused state.

Paul, thanks again, this knowledge really saved my bacon more than a few times, today being the latest.

John
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 20:59
Joined
Aug 30, 2003
Messages
36,125
Glad it helped you John!
 

static

Registered User.
Local time
Today, 04:59
Joined
Nov 2, 2015
Messages
823
The structure of your code helps too. If you split your code up into separate functions that return true if they pass or false if they fail your 'main' procedure is easy to read.

So instead of

Code:
sub ImportFile()
on error goto error_handler
	'stuff to check if file exists
	...
	'stuff to open file 
	...
	'stuff to import file
	...
error_handler:
	erm help! I don't know what caused the error!
end sub

you have

Code:
sub ImportFile()
	somefile = "c:\myfile.txt"
	If FileExists(somefile) then
		if readfile(somefile) then
			if importfile(somefile) then
			   msgbox "file imported"
			else
				msgbox "error importing file"
			end if
		else
			msgbox "couldn't read file"
		end if
	else
		msgbox "file not found"
	end if
end sub

function FileExists(strfile as file) as boolean
	'code to check if file exists
	if fileexists return true else return false
end function

function readfile(strfile as file) as boolean
	...
end function

...
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 23:59
Joined
Apr 27, 2015
Messages
6,337
Agreed. These are tips I have picked up from this site as well. I have a long ways to go, but I have really come a long way since a year ago.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 20:59
Joined
Aug 30, 2003
Messages
36,125
I have really come a long way since a year ago.

You sure have; several thousand miles! :p

I also use that method of having functions return a Boolean. I have an app I'm adding to right now that does a bunch of automated imports from emails. The main function calls different functions depending on the sender (they all have a different way of sending data). Each import function returns a Boolean; the main function tests that and moves the email to a subfolder if true. The error handler in the import function will send me an email with the error specifics plus set the return to false.
 

Users who are viewing this thread

Top Bottom