Possible TRY...CATCH...END TRY

N

Nay Myo Aung

Hi Larry,
what makes you think
a 'small variation' would work?

I just copied and pasted from my other post...

'##### Trying out different servers... #####

Try
Dim objConnection as FakeConnection
objConnection = New FakeConnection
objConnection .ConnectToServer(objDataReader("ServerName"))
Catch
If objDataReader.Read() Then ReTry
Finally
If Not objConnection.Connected Then
MessageBox.Show("Cannot connect to any of the servers")
End If
End Try

'##### END CODE #####

In this case, a small variation is the Server Name.

I hope it helps.

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/
 
H

Herfried K. Wagner [MVP]

Nay Myo Aung said:
I don't know about MFC C++ programmers. But Effiel for .NET has
implemented what I'm currently proposing.

See:
http://docs.eiffel.com/eiffelstudio...guage/20_language/60_exception_mechanism.html

Quoted from the referenced document:

| It should be noted that rescue 'clauses' and 'retry' instructions
| are not something that are used commonly. Out of the
| approximately 2000 class in the delivered Eiffel libraries,
| there are only 16 occurrences. Many of these are oriented
| toward network and database operations for which some
| reasonable recovery might be possible.

If something is not used "commonly", the programming language should IMHO
not provide special commands for it, at least not VB.
 
N

Nay Myo Aung

If something is not used "commonly",

Not commonly in the 2000 classes of Effiel language. However, it certainly
common and comes handy in most of the everyday business software development
projects (at least those I've undertaken).
at least not VB.

Well, shouldn't all dot net langauges provide the same power and features
regardless? :) One shouldn't need to say "I must use this language to do
that" in the .NET world? This is the philosophy of .NET Framework, isn't it?

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/
 
L

Larry Serflaten

Nay Myo Aung said:
Well, shouldn't all dot net langauges provide the same power and features
regardless? :) One shouldn't need to say "I must use this language to do
that" in the .NET world? This is the philosophy of .NET Framework, isn't it?

That was not the philosophy as I understood it. What I heard was that the
langugaes we going to be allowed to diverge so that each can do what is best
for their own target developers. For example, you won't see the unmanaged
power of C++ in managed VB, yet they both are available in Visual Studio .Net.

LFS
 
D

David Pope

I think this thread is example of what could happen if Microsoft implements
the Retry.

An endless thread loop. =0}

Have a great weekend everyone!

David


Nay Myo Aung said:
Hi Jay,

Thanks, I will try input my 2 cents there.

cheers
--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/


Jay B. Harlow said:
Nay,
You may want to use the Suggestion link at the VB.NET 2005 Product
Feedback Center http://lab.msdn.microsoft.com/vs2005/, however I'm not
sure how high a consideration it will get as...

VB.NET already includes it (as Mattias suggests), its been there since
VB.NET 2002. A Number of developers don't like it uses the "Goto"
keyword, however in *this* instance it is only allowed to go from the
Catch Block to the Try Block, so I don't have a big problem with it. Let
me restate that in VB.NET a label in a Try Block can only be reached from
an associated Catch block! In other words I am not referring to uses of
Goto outside of a Catch block or use of a Goto to exit a Try/Catch
statement!

Here is how "ReTry" is currently implemented in VB.NET:
Try Retry:

'...something

Catch e as FileNotFoundException

'correct the error and then Goto Retry

Catch e2 as DirectoryNotFoundException

'correct the error and then Goto Retry

Catch e3 as DriveNotFoundException

'correct the error and then Goto Retry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

"ReTry" as the others have pointed out like any language construct can be
used for good as equally well as evil. I would expect any of my
developers to document very well the actual *need* for ReTry in the
specific context. I normally limit it use to prompting the user:
Try Retry:

'...something

Catch e as FileNotFoundException

If MessageBox.Show("File does not exit!",
Application.ProductName, _
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = DialogResult.Retry
Then
GoTo retry
End If


Hope this helps
Jay

Nay Myo Aung said:
Hi All!

I think I discovered a possible improvement on .NET Framwork's
TRY...CATCH...END TRY statement. I guess most developers might also have
been discovered that at some point in their .NET development process but
continued to ignore the problem due to their workloads.

The word is RETRY.

Consider the following...

Try

'...something

Catch e as FileNotFoundException

'correct the error and then
ReTry

Catch e2 as DirectoryNotFoundException

'correct the error and then
ReTry

Catch e3 as DriveNotFoundException

'correct the error and then
ReTry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

We .NET programmers could use the keyword RETRY in the any of the Catch
block to re-execute the code in Try block. If there's another exception
thrown, we can evaulate that exception in one of the many Catch blocks
until it reached to the 'unkown' exception.

So, I propose that a new keyword ReTry should be incorporated in the
.NET Framework 2 as an improvement.

Feel free to feedback on the idea...

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/
 
N

Nay Myo Aung

Hi David,

Good sense of humor there :)

Seriously, it rather seem as a myth that using ReTry could result in an
endless loop. Usually, it will **not** result in the endless loop. Consider
the following example...

'#### Code ####

Dim strFileName as String

Try
'>> Open a File
OpenFile(strFileName)
Catch
' On error...

'Ask user to input the file name, such as using common dialog...
If objDlg.Show() <> vbCancel Then
strFileName = objDlg.FileName
ReTry
End If
Finally
' Clean up code
End Try

'#### End Code ####

In above example code, it tries to open a file. If there's an error (such as
file not found) the user is asked to input the file name (such as using a
Common Dialog). After entering the file name it will retry. If user click
"cancel" in the Common Dialog, it will exit. Now, where's the endless loop?
:)

It will be looping endlessly only if the user endlessly specifying the wrong
file. It's not the fault of the program. It's the fault of the user. The
user have the option to select "Cancel" if he/she want to break the loop.
The program will only loop because the user wants it to loop.

It is usually common for the need to manage file-open, file-save routines in
GUI based applications. This is one of the areas where ReTry will largely
benefit IMO.

You also have a great weekend too!

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/

David Pope said:
I think this thread is example of what could happen if Microsoft implements
the Retry.

An endless thread loop. =0}

Have a great weekend everyone!

David


Nay Myo Aung said:
Hi Jay,

Thanks, I will try input my 2 cents there.

cheers
--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/


Jay B. Harlow said:
Nay,
You may want to use the Suggestion link at the VB.NET 2005 Product
Feedback Center http://lab.msdn.microsoft.com/vs2005/, however I'm not
sure how high a consideration it will get as...

VB.NET already includes it (as Mattias suggests), its been there since
VB.NET 2002. A Number of developers don't like it uses the "Goto"
keyword, however in *this* instance it is only allowed to go from the
Catch Block to the Try Block, so I don't have a big problem with it. Let
me restate that in VB.NET a label in a Try Block can only be reached
from an associated Catch block! In other words I am not referring to
uses of Goto outside of a Catch block or use of a Goto to exit a
Try/Catch statement!

Here is how "ReTry" is currently implemented in VB.NET:

Try
Retry:

'...something

Catch e as FileNotFoundException

'correct the error and then
Goto Retry

Catch e2 as DirectoryNotFoundException

'correct the error and then
Goto Retry

Catch e3 as DriveNotFoundException

'correct the error and then
Goto Retry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

"ReTry" as the others have pointed out like any language construct can
be used for good as equally well as evil. I would expect any of my
developers to document very well the actual *need* for ReTry in the
specific context. I normally limit it use to prompting the user:

Try
Retry:

'...something

Catch e as FileNotFoundException


If MessageBox.Show("File does not exit!",
Application.ProductName, _
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = DialogResult.Retry
Then
GoTo retry
End If


End Try

Hope this helps
Jay

"Nay Myo Aung" <owNOspaMnerATnaymyoauNOspaMng.name> wrote in message
Hi All!

I think I discovered a possible improvement on .NET Framwork's
TRY...CATCH...END TRY statement. I guess most developers might also
have been discovered that at some point in their .NET development
process but continued to ignore the problem due to their workloads.

The word is RETRY.

Consider the following...

Try

'...something

Catch e as FileNotFoundException

'correct the error and then
ReTry

Catch e2 as DirectoryNotFoundException

'correct the error and then
ReTry

Catch e3 as DriveNotFoundException

'correct the error and then
ReTry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

We .NET programmers could use the keyword RETRY in the any of the Catch
block to re-execute the code in Try block. If there's another exception
thrown, we can evaulate that exception in one of the many Catch blocks
until it reached to the 'unkown' exception.

So, I propose that a new keyword ReTry should be incorporated in the
.NET Framework 2 as an improvement.

Feel free to feedback on the idea...

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/
 
B

Bruno Jouhier [MVP]

You should try Eiffel!

In Eiffel, the exception is rethrown automatically if you don't do anything
special in your catch clause and the only way to prevent the exception from
being rethrown is to do a RETRY! So, this concept exists in other languages.

Bruno.
 
B

Bruno Jouhier [MVP]

Mattias Sjögren said:
Anders,


Checking File.Exists is no excuse for not being prepared to hande a
FileNotFoundException. Just because a file exists at one point doesn't
mean it will still exist when you try to do something with it.


I don't agree, and I know about the atomicity problem (the fact that the
file may disappear between your Exists test and the open call).

My guidelines are the following:

1) You MUST use a try/finally (or better a "using" block) to guarantee that
the file will be closed in a predictable way.

2) In normal circumstances (99.99% of the time), you should NOT introduce
any catch clause around file open logic, you should let exceptions percolate
to a generic catchall that reports and logs all exceptions.

3) There are situations where you should test with Exists before opening and
there are other situations where you should NOT test with Exists.

4) There are some VERY RARE situations where you need both the Exists test
and a try/catch, but normally you don't.

Some words of explanation:

1) I am sure that everybody will agree about this one.

2, 3, and 4). It all boils down to how you draw the line between a "normal"
context of execution and an "abnormal" one.

Here are some assumptions that I consider to be true in a "normal" context
of execution:

a) The vital files that my installer has placed in the Program Files area
have not been deleted by the user. If they have, the user will have to fix
it by restauring the file or reinstalling the application.

b) There is no "sweeper" program that runs in the background and deletes
files in an unpredictable way. So, if my application creates a file and then
tries to open it a few seconds later, the file should still be there. If
there is such a sweeper program then the user should identify the culprit
(virus?) and put his system back into a stable mode.

c) The application may use some optional files. In this case, the
application will have to test if the file exists or not before opening it,
and the application will have a fallback strategy (like using some default
values instead of values read from the file).

d) There may be special situations where the application will use files to
exchange data with another application. If the synchronization protocol
between the two apps is weak, the application must be ready to handle the
case where the file gets deleted by the other app in unpredictable ways (for
example between a call to Exists and a call to open).

So, here are the coding patterns that I recommend in some common scenarios:

When the application needs to open a "vital" file created by the installer,
or a temp file that another method has created just before (cases a and b),
the correct pattern is:
using (Stream stream = File.Open(filename, ...)) {
DoSomething(stream); }

When the application need to work with an "optional" file (case c), the
correct pattern is:
if (File.Exists(filename))
using (Stream stream = File.Open(finename, ...)) {
DoSomething(stream); }
else
UseDefaultValues();
In the very special situation described in d) above (and I would not
recommend using this kind of interprocess communication but sometimes this
is the only way to deal with legacy apps), The correct pattern is:

using (Stream stream = MyFileHelper.TryOpen(filename, ...))
{
if (stream != null)
DoSomething(stream);
}

where MyFileHelper.TryOpen is coded as:

Stream TryOpen(filename, ...)
{
if (!File.Exists(string filename))
return null;
try {
return File.Open(filename, ...);
}
catch (FileNotFoundException ex)
{
return null;
}
}

With these guidelines, you get simple, understandable application code
(instead of a big mess of try/catch), and you know exactly the assumptions
that the developer makes about the files (is it a file that should always
exist? and if it does not, this is just as abnormal as if the disk was bad,
or is it an optional file? or is it a file that may disappear in
unpredictable ways?).

In short, the pattern that you will be using depends on the assumptions that
you make about your operating environment. In many cases, the simplest
pattern (just opening the file without any Exists test nor any catch clause)
is the one to use.

Bruno.
 
H

Herfried K. Wagner [MVP]

Nay Myo Aung said:
Not commonly in the 2000 classes of Effiel language. However, it certainly
common and comes handy in most of the everyday business software
development projects (at least those I've undertaken).

It seems that you are the only one who is missing 'ReTry' ;-)))...
Well, shouldn't all dot net langauges provide the same power and features
regardless? :) One shouldn't need to say "I must use this language to do
that" in the .NET world? This is the philosophy of .NET Framework, isn't
it?

The main goal of .NET was language interoperability based on the CLS, not
providing the same language features in all .NET programming languages.
 
D

David Pope

Nay,

I was playing. I think a Try...Retry(<number of retries>) would work.

I like it.

It would be very useful.

Thanks,

David


Nay Myo Aung said:
Hi David,

Good sense of humor there :)

Seriously, it rather seem as a myth that using ReTry could result in an
endless loop. Usually, it will **not** result in the endless loop.
Consider the following example...

'#### Code ####

Dim strFileName as String

Try
'>> Open a File
OpenFile(strFileName)
Catch
' On error...

'Ask user to input the file name, such as using common dialog...
If objDlg.Show() <> vbCancel Then
strFileName = objDlg.FileName
ReTry
End If
Finally
' Clean up code
End Try

'#### End Code ####

In above example code, it tries to open a file. If there's an error (such
as file not found) the user is asked to input the file name (such as using
a Common Dialog). After entering the file name it will retry. If user
click "cancel" in the Common Dialog, it will exit. Now, where's the
endless loop? :)

It will be looping endlessly only if the user endlessly specifying the
wrong file. It's not the fault of the program. It's the fault of the user.
The user have the option to select "Cancel" if he/she want to break the
loop. The program will only loop because the user wants it to loop.

It is usually common for the need to manage file-open, file-save routines
in GUI based applications. This is one of the areas where ReTry will
largely benefit IMO.

You also have a great weekend too!

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/

David Pope said:
I think this thread is example of what could happen if Microsoft
implements the Retry.

An endless thread loop. =0}

Have a great weekend everyone!

David


Nay Myo Aung said:
Hi Jay,

Thanks, I will try input my 2 cents there.

cheers
--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/


message Nay,
You may want to use the Suggestion link at the VB.NET 2005 Product
Feedback Center http://lab.msdn.microsoft.com/vs2005/, however I'm not
sure how high a consideration it will get as...

VB.NET already includes it (as Mattias suggests), its been there since
VB.NET 2002. A Number of developers don't like it uses the "Goto"
keyword, however in *this* instance it is only allowed to go from the
Catch Block to the Try Block, so I don't have a big problem with it.
Let me restate that in VB.NET a label in a Try Block can only be
reached from an associated Catch block! In other words I am not
referring to uses of Goto outside of a Catch block or use of a Goto to
exit a Try/Catch statement!

Here is how "ReTry" is currently implemented in VB.NET:

Try
Retry:

'...something

Catch e as FileNotFoundException

'correct the error and then
Goto Retry

Catch e2 as DirectoryNotFoundException

'correct the error and then
Goto Retry

Catch e3 as DriveNotFoundException

'correct the error and then
Goto Retry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

"ReTry" as the others have pointed out like any language construct can
be used for good as equally well as evil. I would expect any of my
developers to document very well the actual *need* for ReTry in the
specific context. I normally limit it use to prompting the user:

Try
Retry:

'...something

Catch e as FileNotFoundException


If MessageBox.Show("File does not exit!",
Application.ProductName, _
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) =
DialogResult.Retry Then
GoTo retry
End If


End Try

Hope this helps
Jay

"Nay Myo Aung" <owNOspaMnerATnaymyoauNOspaMng.name> wrote in message
Hi All!

I think I discovered a possible improvement on .NET Framwork's
TRY...CATCH...END TRY statement. I guess most developers might also
have been discovered that at some point in their .NET development
process but continued to ignore the problem due to their workloads.

The word is RETRY.

Consider the following...

Try

'...something

Catch e as FileNotFoundException

'correct the error and then
ReTry

Catch e2 as DirectoryNotFoundException

'correct the error and then
ReTry

Catch e3 as DriveNotFoundException

'correct the error and then
ReTry

Catch '<< catch 'unknown' error

'log the error

Finally

End Try

We .NET programmers could use the keyword RETRY in the any of the
Catch block to re-execute the code in Try block. If there's another
exception thrown, we can evaulate that exception in one of the many
Catch blocks until it reached to the 'unkown' exception.

So, I propose that a new keyword ReTry should be incorporated in the
.NET Framework 2 as an improvement.

Feel free to feedback on the idea...

--
Nay Myo Aung
Chief Visual Software Architect
MCP MCSD MCDBA

Email: owN0SPAMner @naymyoauN0SPAMng.name [remove NOSPAM s]
Homepage: http://hyperdisc.unitec.ac.nz/postgrad/aungn01/
 

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


Top