473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about Try - Catch codes.

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: pd*********************@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

Nov 21 '05 #1
5 1743
Patrick Dickey wrote:
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
///

--

(O) e n o n e
Nov 21 '05 #2
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
--
Terry Burns
http://TrainingOn.net
"Patrick Dickey" <pd*********************@msn.com> wrote in message
news:Os**************@TK2MSFTNGP14.phx.gbl...
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:
pd*********************@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

Nov 21 '05 #3
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: pd*********************@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

Nov 21 '05 #4
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" <pd*********************@msn.com> wrote in message
news:eb**************@TK2MSFTNGP15.phx.gbl...
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:
pd*********************@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

Nov 21 '05 #5
"Patrick Dickey" <pd*********************@msn.com> wrote in message
news:Os**************@TK2MSFTNGP14.phx.gbl...
.. . .
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.
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fb | last post by:
Hi everyone. I have the following code. It was a question out of C++ how to program. I get a warning about "possible unreachable code" in the RunCode() function, but I don't see any problem with...
9
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, When a good developer should declare an exception variable in a catch clause? Consider an example: try { // Do something potentially dangerous... } catch(SomeException e)
7
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
1
by: chen | last post by:
We're having an internal debate about the merits & demerits of returning status codes in the output message vs exceptions to signify errors in handling a Web method. The status code camp is...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
4
by: | last post by:
Hello Group I have a button click sub, which calles a function called 'send' to send over a tcp connection. This function can call an exception. If an exception occurs, I need to quit the button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.