Possible TRY...CATCH...END TRY

This is a discussion on Possible TRY...CATCH...END TRY within the Framework and Interface Programming forums in category; 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 ...

Go Back   Application Development Forum > Framework and Interface Programming

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 12-03-2004, 04:36 AM
Nay Myo Aung
Guest
 
Default Possible TRY...CATCH...END TRY

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/


Reply With Quote
  #2  
Old 12-03-2004, 05:00 AM
Robby
Guest
 
Default Re: Possible TRY...CATCH...END TRY


This is a great idea!!

We VB programmers have always had On Error Goto <label> where we could try
to fix the error and go back to the start of the block. While we can still
do this in .Net I prefer not to as I try to use the framework error handling
of Try ... Catch ... End Try. This idea offers some really great
flexibility for handling exceptions. I am surprised that they have not
mentioned anything like this in their .Net briefs.

If your listening MS, please consider this.

Robby


"Nay Myo Aung" <owNOspaMnerATnaymyoauNOspaMng.name> wrote in message
news:uifBpxR2EHA.3452@TK2MSFTNGP14.phx.gbl...
> 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/
>



Reply With Quote
  #3  
Old 12-03-2004, 05:11 AM
Elp
Guest
 
Default Re: Possible TRY...CATCH...END TRY

On Fri, 3 Dec 2004 22:36:16 +1300, Nay Myo Aung wrote:

> 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.


The first thing that catched my eye when i saw your idea is that it would
be dead easy to enter an endless loop with this. Retry again and again
without any way to stop this. So you would need to add a counter in each of
your catch block to check if this block is not being executed endlessly.
Quite messy, isn't it?
Reply With Quote
  #4  
Old 12-03-2004, 05:35 AM
=?Utf-8?B?UmFrZXNoIFJhamFu?=
Guest
 
Default Re: Possible TRY...CATCH...END TRY

Hi,

Maybe a facility something like
Retry(2)
for retrying 2 times might help.

Of course, the call to Retry should be specific for the exception type.

Regards,
Rakesh Rajan

"Elp" wrote:

> On Fri, 3 Dec 2004 22:36:16 +1300, Nay Myo Aung wrote:
>
> > 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.

>
> The first thing that catched my eye when i saw your idea is that it would
> be dead easy to enter an endless loop with this. Retry again and again
> without any way to stop this. So you would need to add a counter in each of
> your catch block to check if this block is not being executed endlessly.
> Quite messy, isn't it?
>

Reply With Quote
  #5  
Old 12-03-2004, 05:40 AM
Anders Norås [MCAD]
Guest
 
Default Re: Possible TRY...CATCH...END TRY

> 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
> ....


I don't think this is a good idea. There is a good reason why exceptions are
called exceptions. They only occur exceptionally.
You should not be surprised by FileNotFoundExceptions and similar in your
code. The correct way to handle the file not found issue in your example is
to do it like this:

if (File.Exists(filename)) {
// Do something
} else {
// Try another filename or something..
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras


Reply With Quote
  #6  
Old 12-03-2004, 05:55 AM
=?Utf-8?B?UmFrZXNoIFJhamFu?=
Guest
 
Default Re: Possible TRY...CATCH...END TRY

Hi,

This is true.

But in some cases, when you know it's possible some exception might be
thrown, and you have implemented code to handle it (in a catch block), you
might want to retry what you just did. In those cases, a retry keyword might
be better.

Regards,
Rakesh Rajan

"Anders Norås [MCAD]" wrote:

> > 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
> > ....

>
> I don't think this is a good idea. There is a good reason why exceptions are
> called exceptions. They only occur exceptionally.
> You should not be surprised by FileNotFoundExceptions and similar in your
> code. The correct way to handle the file not found issue in your example is
> to do it like this:
>
> if (File.Exists(filename)) {
> // Do something
> } else {
> // Try another filename or something..
> }
>
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras
>
>
>

Reply With Quote
  #7  
Old 12-03-2004, 06:00 AM
Morten Wennevik
Guest
 
Default Re: Possible TRY...CATCH...END TRY

Hi Nay Myo Aung,

You can have this functionality today

int counter = 0;
start:;
try
{
File.Open("dummy", FileMode.Open);
}
catch
{
counter++;
if(counter < 5)
goto start;
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Reply With Quote
  #8  
Old 12-03-2004, 06:03 AM
Cor Ligthert
Guest
 
Default Re: Possible TRY...CATCH...END TRY

Nay,

Why would you endless have to catch an error is one them not more than
enough

The only thing where I can now see where it can be used is in spaghetti
code.
(And your proposal would than be a legalizing of that)

You are not the first one by the way who contributes this to the language.vb
newsgroup.

Just my thought


Cor


"Nay Myo Aung" <owNOspaMnerATnaymyoauNOspaMng.name>

> 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/
>



Reply With Quote
  #9  
Old 12-03-2004, 07:09 AM
Ollie
Guest
 
Default Re: Possible TRY...CATCH...END TRY

this is just messy IMO

Ollie

"Rakesh Rajan" <RakeshRajan@discussions.microsoft.com> wrote in message
news:8DEF66C7-06FD-4564-8146-C2C9C0A188FE@microsoft.com...
> Hi,
>
> Maybe a facility something like
> Retry(2)
> for retrying 2 times might help.
>
> Of course, the call to Retry should be specific for the exception type.
>
> Regards,
> Rakesh Rajan
>
> "Elp" wrote:
>
> > On Fri, 3 Dec 2004 22:36:16 +1300, Nay Myo Aung wrote:
> >
> > > 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.

> >
> > The first thing that catched my eye when i saw your idea is that it

would
> > be dead easy to enter an endless loop with this. Retry again and again
> > without any way to stop this. So you would need to add a counter in each

of
> > your catch block to check if this block is not being executed endlessly.
> > Quite messy, isn't it?
> >



Reply With Quote
  #10  
Old 12-03-2004, 07:24 AM
Nay Myo Aung
Guest
 
Default Re: Possible TRY...CATCH...END TRY

Hi Cor,

Actually, it is not endless. We could put counters or we could have
something like ReTry(3) for 3 retries, as pointed out by Rakesh Rajan.

The main idea is to eliminate the need to rewrite the code that is already
written in the Try block with only a small variation (small enough to be
defined through variables).

For instance...

Try
<<TaskA>>
Catch

Try
<<TaskA with small variation>>
Catch

Try
<<TaskA with another small variation>>
Catch

Try
<<TaskA with yet another small variation>>
Catch

End Try

End Try

End Try

End Try

In above example, we need to duplicate the code written in Try block into
sub-Try blocks with just small variations. I consider this as a bad practice
since more code duplication means, more errors and more maintenance
overhead. That's why we need a ReTry statement.

I know we can put the reusable code into Sub procedures but sometimes the
code is not big enough (or not worth the system overhead) to put it in its
own procedure.

--
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/


"Cor Ligthert" <notmyfirstname@planet.nl> wrote in message
news:%2331sudS2EHA.1408@TK2MSFTNGP10.phx.gbl...
> Nay,
>
> Why would you endless have to catch an error is one them not more than
> enough
>
> The only thing where I can now see where it can be used is in spaghetti
> code.
> (And your proposal would than be a legalizing of that)
>
> You are not the first one by the way who contributes this to the
> language.vb newsgroup.
>
> Just my thought
>
>
> Cor
>
>
> "Nay Myo Aung" <owNOspaMnerATnaymyoauNOspaMng.name>
>
>> 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/
>>

>
>



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:09 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.