Link to home
Start Free TrialLog in
Avatar of prabhuram
prabhuram

asked on

exception handling...

hi.. i'm doing a project in VB6.0 not in .net..  when i run the application.. if an error comes like ADO not supported or on the occasion of any fatal errors.. the application comes out of execution.. so what i want is.. whenever an unexpected errors like this happening.. i want to print an error msg.. it'll be so cute then.. so any explanations..

PS: dont advice me to go for VB.net as i have completed 85% of the project in VB6.0 and i dont want to touch VB.net..
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Use the Err Object to show the error message.

You can write the error message into a Log file using Error Handling Tags. then Print or view it from it.

Or simply:

Printer.Print Err.Description ?
Avatar of sridhar_PJ
sridhar_PJ

Hi,
As ryancys said try to use err object.
use
On Error Goto Err:
in top of your code.

In end of code
Err:
Msgbox Err.Number
Msgbox Err.Description

Regards
SRidhar



Avatar of prabhuram

ASKER

give me a sample code for doing that..  i mean write a simple subfunction and try to catch the exception in that block.. thanks for your quick reply..
give me a sample code for doing that..  i mean write a simple subfunction and try to catch the exception in that block.. thanks for your quick reply..
Hi,
Just a simple example


Private Sub Command1_Click()
Dim x As Integer
On Error GoTo Err:
x = "Hello"        'Passing a sting in integer datatype

Err:
MsgBox Err.Number
MsgBox Err.Description

End Sub

What ever error occurs in the block, the focus will goto Err: and you can do the error handling there ie. you can use Resume Next to bypass the error or u can Log your Report.

Regards
Sridhar
I don't believe I would use Err as a lable and in any case you will need to exit the sub before the error handler for cases that don't generate an error.  I always include a resume statement as well:

Private Sub Command1_Click()
Dim x As Integer
On Error GoTo ErrorTrap:
x = "Hello"        'Passing a sting in integer datatype

Exit Sub

ErrorTrap:
MsgBox Err.Number
MsgBox Err.Description
Resume ErrorExit

ErrorExit:

End Sub
In my opinion, Resume ErrorExit would be superfluous.

Something like this should do the job, a combination of sridhar_PJ and Joe_Griffith :-

Dim x As Integer
On Error GoTo ErrorTrap:
  x = "Hello"        'Passing a sting in integer datatype
Exit Sub

ErrorTrap:
  MsgBox Err.Number & vbtab & Err.Description
  '-- Or you could use Debug.Print statements to print the errors in the Immediate Window:- Debug.Print Err.Description
End Sub
In this case the resume doesn't do much but my point was that I think there should be a resume for every error.  I've found that while it may work for a while those unresolved errors are piling up somewhere and eventually cause problems.  In any case he will probably want to resume somewhere else.

For what it is worth this guy is a pretty stingy grader:
Questions Asked 30
Last 10 Grades Given B B A B C C C C C C  
hi  Joe_Griffith,
if you arent in a mood to help me.. please keep quiet.. its not fair telling these things.. i'm now ok and can you see my grading for last 4 questions?? and to all..

according to your answers.. its very good solution.. but what i want is;; i have some 200 sub-functions and if i include all these 5+ lines in those modules.. it becomes too large and more than that i dont have privileges for touching some of the modules.. so what i want to have is .. can i have some thing like global function for handling these errors.. it would be so good..

or any other alternative suggestions for this scenerio..

ps: i'll surely grade this qn and just wait until the qn is locked..
You would run into problems of masking the errors and would not be able get to the root of cause of errors. In my opinion it would be wise you have Error Handlers in each module.
One more suggestion, what you can do is have a variable declared in your main function,say strTraceError. After making a call to each module(even the ones you dont have privelege)you could increment strTraceError value. Something like this:-

----------your main function----------
Dim strTraceError As String


On Error Goto ErrHandler

Call ModuleA(x)
strTraceError = "1"

Call ModuleB(y)
strTraceError = "2"

.
.
.
Exit Sub
ErrHandler:
s = "Error at Step: " & strTraceError
App.LogEvent s
Exit Sub

PS:- App.LogEvent functionality is only available when running a compiled application. If you attempt to use the logging methods while in the Microsoft Visual Basic development environment, the logging methods will be ignored.
ASKER CERTIFIED SOLUTION
Avatar of rpai
rpai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial