Question about Try - Catch codes.

P

Patrick Dickey

Hello, All!
I'm modifying some source code that I downloaded from
Planet-source-code a while back, and have a question about Try Catch
statements. Basically, I'm wondering if I can do a Try with multiple Catch
statements, or do I have to figure out how to embed Try Catch statements
into my code. The scenario is this (and I'll paste the actual code at the
bottom of this post). I'm modifying a web browser, and in the About Help
box, it has the "System Info" button. In the System Info button, there are
some GO TO statements that I want to get rid of. Plus, the SysInfoError
which is referred to by the GO TO statements doesn't tell you anything
except that an error happened.
What I'd like to do is put a Try at the beginning of the If-Then
statements, and for each possible error, put a Catch statement with a
message saying what the error is. I have a feeling, if I can't do that,
then a somewhat major rewrite of the subroutine is in order. Here's the
subroutine, and I appreciate any help that I can get. Also, If I end up
using snippets of code from here, I will definitely credit the poster.

Public Sub StartSysInfo()

On Error GoTo SysInfoErr


Dim rc As Integer

Dim SysInfoPath As String


' Try To Get System Info Program Path\Name From Registry...

If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO,
SysInfoPath) Then

' Try To Get System Info Program Path Only From Registry...

ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC, gREGVALSYSINFOLOC,
SysInfoPath) Then

' Validate Existance Of Known 32 Bit File Version

'UPGRADE_WARNING: Dir has a new behavior. Click for more:
'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1041"'

If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then

SysInfoPath = SysInfoPath & "\MSINFO32.EXE"


' Error - File Can Not Be Found...

Else

GoTo SysInfoErr

End If

' Error - Registry Entry Can Not Be Found...

Else

GoTo SysInfoErr

End If


Call Shell(SysInfoPath, AppWinStyle.NormalFocus)


Exit Sub

SysInfoErr:

MsgBox("System Information Is Unavailable At This Time", MsgBoxStyle.OKOnly)

End Sub

Thank you again in advance.
With best regards, Patrick Dickey. E-mail: pd1ckey43**RemoveThis**@msn.com




---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0521-3, 05/26/2005
Tested on: 5/27/2005 1:27:00 AM
avast! - copyright (c) 1988-2005 ALWIL Software.
http://www.avast.com
 
T

Terry Burns

These two methods of error handling are completely different. The Try Catch
model relies upon exception handling, in other words when an exception is
thrown by a class or method, this can be seen inside the Try block and
caught by one or more Catch blocks. See example below.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try

throwit1()

throwit2()

Catch ex As Exp1

MsgBox("1")

Catch ex As Exp2

MsgBox("2")

End Try

End Sub

Sub throwit1()

Throw New Exp1

End Sub

Sub throwit2()

Throw New Exp2

End Sub

If however you insist on using the OnError model, then you could OnErro Goto
YourErrorHandlingCode: and use the Select Case Statement

Select Case YourErrorNumber
Case nn
Case nn
Case Else

End Case

This is a simplified answer, but it should put u in the right direction

HTH
 
O

Oenone

Patrick said:
Basically, I'm wondering if I can do a Try with multiple
Catch statements

Yes you can. For example:

\\\
Try
[your code here]

Catch ex As OverflowException
[handle this type of error]

Catch ex As NullReferenceException
[handle this type of error]

Catch ex As Exception
[handle all other error types]

End Try
///
 
P

Patrick Dickey

Hello, Patrick!
You wrote to All on Fri, 27 May 2005 01:26:58 -0500:

[Sorry, skipped to conserve space]

PD> Public Sub StartSysInfo()
PD>
PD> On Error GoTo SysInfoErr
PD>

PD> Dim rc As Integer
PD>
PD> Dim SysInfoPath As String
PD> ' Try To Get System Info Program Path\Name From Registry...
PD>
PD> If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO,
PD>
PD> SysInfoPath) Then
PD> ' Try To Get System Info Program Path Only From
PD> Registry...
PD> ElseIf GetKeyValue(HKEY_LOCAL_MACHINE,
PD> gREGKEYSYSINFOLOC,
PD> gREGVALSYSINFOLOC,
PD> SysInfoPath) Then
PD> '
PD> Validate Existance Of Known 32 Bit File Version
PD> 'UPGRADE_WARNING: Dir
PD> has a new behavior. Click for more:
PD>
PD> 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="
PD>
PD> vbup1041"'
PD> If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
PD>
PD> SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
PD> ' Error - File Can Not Be
PD> Found...
PD> Else
PD> GoTo SysInfoErr
PD> End If
PD> ' Error - Registry
PD> Entry Can Not Be Found...
PD> Else
PD> GoTo SysInfoErr
PD> End If

PD> Call
PD> Shell(SysInfoPath, AppWinStyle.NormalFocus)
PD> Exit Sub
PD>
PD> SysInfoErr:
PD> MsgBox("System Information Is Unavailable At This
PD> Time",
PD> MsgBoxStyle.OKOnly)
PD> End Sub

[Sorry, skipped to conserve space]

Ok, I'm going to revamp this a little bit. For Terry's reply, I need to
clarify that I'm trying to eliminate all GOTO's-- Including the OnError Goto
statement. Bascially, I've ran a project analyzer on the code, and the
GOTO's are one thing it flagged. Plus, waaaaaaaaaaaaay back when I learned
programming (COBOL, Pascal, ApplesoftBASIC, FORTRAN, etc.) I was taught that
there is almost always a better way of coding then using GOTO. GOTO is
evil.. And, in most cases, the instructor would deduct points for GOTO
statements. Spaghetti programming, is the term we used for GOTO's.

So, here's what I'm thinking about doing. I'll recopy only the appropriate
IF-Then statements, and the end of the subroutine, and get everyone's
opinions. Thank you again, in advance.
Dim ErrorLevel as Integer
ErrorLevel = 0
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO,
SysInfoPath) Then
' Try To Get System Info Program Path Only From Registry...
ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC,
gREGVALSYSINFOLOC, SysInfoPath) Then
' Validate Existance Of Known 32 Bit File Version
If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
Else
' Error - File Can Not Be Found...
ErrorLevel = 2
End If
Else
ErrorLevel = 1
' Error - Registry Entry Can Not Be Found...
End If

IF ErrorLevel = 1 Then
MsgBox("The Registry entry could not be found.", MsgBoxStyle.OKOnly)
ElseIF ErrorLevel = 2 Then
MsgBox("The Program File could not be found.", MsgBoxStyle.OKOnly)
Endif
EndIf

Basically, the only changes I want to make are creating an integer called
ErrorLevel, initializing it to 0 at the beginning, and then setting a value
for it, instead of using the "GOTO SysError" statements, in the IF-Then
statements.

With best regards, Patrick Dickey. E-mail: pd1ckey43**RemoveThis**@msn.com




---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0522-1, 05/31/2005
Tested on: 5/31/2005 5:37:11 AM
avast! - copyright (c) 1988-2005 ALWIL Software.
http://www.avast.com
 
T

Terry Burns

I would be more inclined to create an exception expecially for this and
throw it in your GetKeyValue calling function. Think about an approach which
looks something along these lines.

Try

GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO,
gREGVALSYSINFO,SysInfoPath) Then
GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC,gREGVALSYSINFOLOC,
SysInfoPath) Then

If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
Else
Throw new FileNotFoundException . . . . . . . . . stuff . . . . . .
End If

Catch ex as GetKeyValueException

MsgBox ex.message ' or Some error handling code.

Catch ex as FileNotFoundException

End Try

--
Terry Burns
http://TrainingOn.net
Patrick Dickey said:
Hello, Patrick!
You wrote to All on Fri, 27 May 2005 01:26:58 -0500:

[Sorry, skipped to conserve space]

PD> Public Sub StartSysInfo()
PD>
PD> On Error GoTo SysInfoErr
PD>

PD> Dim rc As Integer
PD>
PD> Dim SysInfoPath As String
PD> ' Try To Get System Info Program Path\Name From Registry...
PD>
PD> If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO,
PD>
PD> SysInfoPath) Then
PD> ' Try To Get System Info Program Path Only From
PD> Registry...
PD> ElseIf GetKeyValue(HKEY_LOCAL_MACHINE,
PD> gREGKEYSYSINFOLOC,
PD> gREGVALSYSINFOLOC,
PD> SysInfoPath) Then
PD> '
PD> Validate Existance Of Known 32 Bit File Version
PD> 'UPGRADE_WARNING: Dir
PD> has a new behavior. Click for more:
PD>
PD> 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="
PD>
PD> vbup1041"'
PD> If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
PD>
PD> SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
PD> ' Error - File Can Not Be
PD> Found...
PD> Else
PD> GoTo SysInfoErr
PD> End If
PD> ' Error - Registry
PD> Entry Can Not Be Found...
PD> Else
PD> GoTo SysInfoErr
PD> End If

PD> Call
PD> Shell(SysInfoPath, AppWinStyle.NormalFocus)
PD> Exit Sub
PD>
PD> SysInfoErr:
PD> MsgBox("System Information Is Unavailable At This
PD> Time",
PD> MsgBoxStyle.OKOnly)
PD> End Sub

[Sorry, skipped to conserve space]

Ok, I'm going to revamp this a little bit. For Terry's reply, I need to
clarify that I'm trying to eliminate all GOTO's-- Including the OnError
Goto statement. Bascially, I've ran a project analyzer on the code, and
the GOTO's are one thing it flagged. Plus, waaaaaaaaaaaaay back when I
learned programming (COBOL, Pascal, ApplesoftBASIC, FORTRAN, etc.) I was
taught that there is almost always a better way of coding then using GOTO.
GOTO is evil.. And, in most cases, the instructor would deduct points for
GOTO statements. Spaghetti programming, is the term we used for GOTO's.

So, here's what I'm thinking about doing. I'll recopy only the
appropriate IF-Then statements, and the end of the subroutine, and get
everyone's opinions. Thank you again, in advance.
Dim ErrorLevel as Integer
ErrorLevel = 0
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFO, gREGVALSYSINFO,
SysInfoPath) Then
' Try To Get System Info Program Path Only From Registry...
ElseIf GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYSYSINFOLOC,
gREGVALSYSINFOLOC, SysInfoPath) Then
' Validate Existance Of Known 32 Bit File Version
If (Dir(SysInfoPath & "\MSINFO32.EXE") <> "") Then
SysInfoPath = SysInfoPath & "\MSINFO32.EXE"
Else
' Error - File Can Not Be Found...
ErrorLevel = 2
End If
Else
ErrorLevel = 1
' Error - Registry Entry Can Not Be Found...
End If

IF ErrorLevel = 1 Then
MsgBox("The Registry entry could not be found.", MsgBoxStyle.OKOnly)
ElseIF ErrorLevel = 2 Then
MsgBox("The Program File could not be found.", MsgBoxStyle.OKOnly)
Endif
EndIf

Basically, the only changes I want to make are creating an integer called
ErrorLevel, initializing it to 0 at the beginning, and then setting a
value for it, instead of using the "GOTO SysError" statements, in the
IF-Then statements.

With best regards, Patrick Dickey. E-mail:
pd1ckey43**RemoveThis**@msn.com



---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0522-1, 05/31/2005
Tested on: 5/31/2005 5:37:11 AM
avast! - copyright (c) 1988-2005 ALWIL Software.
http://www.avast.com
 
P

Phill. W

.. . .
Basically, I'm wondering if I can do a Try with multiple Catch
statements,

Yes you can, with one proviso - you have to "know" which
Exceptions derive from which other Exceptions and make sure
that you Catch the "smallest" one first, as in

Try
' Do Something database-y
Catch ox as System.Data.Odbc.OdbcException
Catch sx as System.SystemException
Catch ex as System.Exception
Finally
End Try

Each of these Exception classes is inherited from the next and, when
an Exception is thrown, the program will use the "most specific"
Exception Handler. If you were to put

Catch ex as System.Exception

first, that's the /only/ handler that would ever get run.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Try ... Catch Loop 7
Try...Catch question 4
Try Catch Statement help 4
Try/Catch question 8
try... catch question. 7
try-catch question 2
try catch question 12
Multiple Catches in Try/Catch 3

Top