Article View: borland.public.csharpbuilder.language.csharp
Article #619Re: try catch question
From: Colonel Tony
Date: Sat, 17 Jul 2004 14:16
Date: Sat, 17 Jul 2004 14:16
71 lines
3162 bytes
3162 bytes
Hi Jon,
Actually it doesn't work, you have to pre declare your objects outside of
the try catch.
and then create inside the try catch, unlike delphi everything you want to
catch actually has to be in the block.
This code will fail because it wont be able to find the mywriter.close()
try { StreamWriter mywriter new StreamWriter("pathtofile"); }
catch {}
finally { mywriter.Close(); }
this code will work, but wont catch the exception
StreamWriter mywriter new StreamWriter("pathtofile");
try {}
catch {}
finally { mywriter.Close(); }
but this one will work and catch the exception
StreamWriter mywriter;
try { mywriter new StreamWriter("pathtofile"); }
catch {}
finally { mywriter.Close(); }
I have verified this behavior on win32 and mono by putting a invalid path in
the stream writer, an if it's not done like the last example it won't catch
the exception.
"Jon Shemitz" <jon@midnightbeach.com> wrote in message
news:40F380DB.819CE161@midnightbeach.com...
> Tony Caduto wrote:
>
> > StreamWriter mywriter new StreamWriter("pathtofile");
> > try {}
> > catch {}
> > finally { mywriter.Close(); }
> >
> > I get a error because it can't find mywriter in the catch or finally
> > section.
> > It seems like the try catch finally is in it's own namespace.
>
> This isn't valid skeleton code - you need
> StreamWriter mywriter = new StreamWriter("pathtofile");
> not
> StreamWriter mywriter new StreamWriter("pathtofile");
>
> Based on that error, I'm going to GUESS that what you really coded was
>
> try { StreamWriter mywriter new StreamWriter("pathtofile"); }
> catch {}
> finally { mywriter.Close(); }
>
> and the issue is that mywriter is only in scope within the try block.
>
> StreamWriter mywriter new StreamWriter("pathtofile");
> try {}
> catch {}
> finally { mywriter.Close(); }
>
> should work ... as should
>
> using (StreamWriter mywriter new StreamWriter("pathtofile"))
> try {}
> catch {}
>
> --
>
> programmer, author http://www.midnightbeach.com
> and father http://www.midnightbeach.com/hs
Technical Details
Message-ID:
<40f934bb$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!stdio!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<40f3748e@newsgroups.borland.com> <40F380DB.819CE161@midnightbeach.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,f80ee75cfac101b9 X-Google-Attributes: gid1efe35,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Colonel Tony" <tcaduto@amsoftwaredesign.com> From: "Colonel Tony" <tcaduto@amsoftwaredesign.com> Newsgroups: borland.public.csharpbuilder.language.csharp References: <40f3748e@newsgroups.borland.com> <40F380DB.819CE161@midnightbeach.com> Subject: Re: try catch question Date: Sat, 17 Jul 2004 09:16:33 -0500 Lines: 73 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 65.26.202.116 Message-ID: <40f934bb$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1090073787 65.26.202.116 (17 Jul 2004 07:16:27 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!stdio!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail