473,320 Members | 1,838 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,320 software developers and data experts.

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

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

Nov 16 '05 #1
6 3961
Hi Kids,

Yes, it can throw 9 different exceptions.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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
jo*********@gmx.net
Nov 16 '05 #3
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" <Mo************@hotmail.com> wrote in message
news:op**************@morten-xp.contoso7.com...
Hi Kids,

Yes, it can throw 9 different exceptions.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4
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
jo*********@gmx.net
Nov 16 '05 #5
Unfortunately I haven't got one yet.
Would you recommend a few good practise?

"Joerg Jooss" <jo*********@gmx.net> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
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
jo*********@gmx.net

Nov 16 '05 #6
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...bexcept.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
jo*********@gmx.net
Nov 16 '05 #7

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

Similar topics

1
by: Vladimir Bezugliy | last post by:
I have a server that listen a socket. And I have next client: TcpClient socketForServer = new TcpClient("IVBEZUGLIY", 21000); NetworkStream networkStream = socketForServer.GetStream();...
6
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
0
by: LongRunner | last post by:
Hihi, i am writing utilities classes using C#. The utilities contain my own exceptional classes. Until now, my design about the exceptional classes is that: I have my expceitonal class and...
7
by: thompson_38 | last post by:
I have a COM object that wraps a console application. This console app outputs strings, using tprintf statements, throughout execution. I would like to capture that output from the console app...
10
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the...
1
by: needin4mation | last post by:
Hi, please consider this snippet: string filename "c:\\inetpub\\wwwroot\\getdirections\\Directions.xml"; XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList authorList =...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
31
by: Zytan | last post by:
There is no IsDisposed() method. I could just access it, and catch ObjectDisposedException, but that seems ugly. Zytan
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.