PC Review


Reply
Thread Tools Rate Thread

capture using(StreamWriter wr = new StreamWriter()) exceptional

 
 
kids_pro
Guest
Posts: n/a
 
      2nd Sep 2004
Hi there,

using(StreamWriter wr = new
StreamWriter(filename,false,Encoding.UTF8,1024)){}

Will the line above throw any exception when it can't create file?
What is the best way to capture it exceptions?

Cheers,
Kids



 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      2nd Sep 2004
Hi Kids,

Yes, it can throw 9 different exceptions.

--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
Joerg Jooss
Guest
Posts: n/a
 
      2nd Sep 2004
kids_pro wrote:
> Hi there,
>
> using(StreamWriter wr = new
> StreamWriter(filename,false,Encoding.UTF8,1024)){}
>
> Will the line above throw any exception when it can't create file?
> What is the best way to capture it exceptions?


"using" is basically a try/finally block, thus all exceptions thrown by
StreamWriter as documented on MSDN will bubble up to the next exception
handler. If you want to deal with certain exceptions right there, it's
easier to use a classical try/catch/finally.


Cheers,

--
Joerg Jooss
(E-Mail Removed)


 
Reply With Quote
 
kids_pro
Guest
Posts: n/a
 
      2nd Sep 2004
Wow amazing.
I better not use the using statement forever, right?
Cuz there is no good to write a few line and capture 9 exceptions

"Morten Wennevik" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Hi Kids,
>
> Yes, it can throw 9 different exceptions.
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]



 
Reply With Quote
 
Joerg Jooss
Guest
Posts: n/a
 
      2nd Sep 2004
kids_pro wrote:
> Wow amazing.
> I better not use the using statement forever, right?
> Cuz there is no good to write a few line and capture 9 exceptions


It depends on your exception handling strategy -- hopefully you have one ;-)

--
Joerg Jooss
(E-Mail Removed)


 
Reply With Quote
 
kids_pro
Guest
Posts: n/a
 
      3rd Sep 2004
Unfortunately I haven't got one yet.
Would you recommend a few good practise?

"Joerg Jooss" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> kids_pro wrote:
> > Wow amazing.
> > I better not use the using statement forever, right?
> > Cuz there is no good to write a few line and capture 9 exceptions

>
> It depends on your exception handling strategy -- hopefully you have one

;-)
>
> --
> Joerg Jooss
> (E-Mail Removed)
>
>



 
Reply With Quote
 
Joerg Jooss
Guest
Posts: n/a
 
      3rd Sep 2004
kids_pro wrote:
> Unfortunately I haven't got one yet.
> Would you recommend a few good practise?


This is a very complex topic, and I guess the world is still waiting for
ultimate treatise on exception handling. Here is a nice checklist, though
(see http://www-106.ibm.com/developerwork...ejbexcept.html):

>>

1. If you can't handle an exception, don't catch it.

2. If you catch an exception, don't swallow it.

3. Catch an exception as close as possible to its source.

4. Log an exception where you catch it, unless you plan to rethrow it.

5. Structure your methods according to how fine-grained your exception
handling must be.

6. Use as many typed exceptions as you need, particularly for application
exceptions.

Point 1 is obviously in conflict with Point 3. The practical solution is a
trade-off between how close to the source you catch an exception and how far
you let it fall before you've completely lost the intent or content of the
original exception.
<<

Also, the chapter on exceptions in Richter's .NET Framework Programming (MS
Press) is highly recommended.

Cheers,

--
Joerg Jooss
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference in Filestream + StreamWriter and just StreamWriter =?Utf-8?B?aXdkdTE1?= Microsoft VB .NET 1 2nd Aug 2006 01:40 PM
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces Daniel Microsoft Dot NET 3 8th Sep 2005 06:47 PM
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces Daniel Microsoft C# .NET 3 8th Sep 2005 03:07 PM
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces Daniel Microsoft Dot NET Framework 0 8th Sep 2005 01:40 AM
ASP.NET C# WriteToFile StreamWriter error: 'StreamWriter' could not be found rex64 Microsoft C# .NET 4 15th Jun 2005 03:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:03 PM.