473,804 Members | 3,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi there,

using(StreamWri ter wr = new
StreamWriter(fi lename,false,En coding.UTF8,102 4)){}

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 3989
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(StreamWri ter wr = new
StreamWriter(fi lename,false,En coding.UTF8,102 4)){}

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*********@gm x.net> wrote in message
news:eG******** *****@TK2MSFTNG P09.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
3572
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(); StreamWriter streamWriter = new StreamWriter(networkStream); streamWriter.Write("Hello12345!!!"); streamWriter.Flush(); //streamWriter.Close();
6
1916
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 used this form. Recently, someone asked me why an int was needed, since -1 (usually EOF) can be safely represented in a signed char value. In fact, any negative number can be safely represented in a signed char value, and wouldn't conflict with...
0
1135
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 inherit from ApplicationException and i will assign a message to it when creating the instance for more details to identify what's the problems are. The message is not hardcoded in program, the exceptional classes will call and pass MessageKey to a...
7
2370
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 and display it in my C# Windows.Forms test application. I have tried StreamWriter, StringWriter, OpenStandardOutput, but I only get empty strings returned. I do not want to change the console application nor the COM app. I am still investigating,...
10
4662
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 "Encoding.UTF8" in the constructor like this: StreamWriter writer = new StreamWriter(file, Encoding.UTF8); When I do this the StreamWriter inserts an UTF-8 preamble "" into the
1
1293
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 = doc.SelectNodes("/NewDataSet/location"); StreamWriter writer = new StreamWriter(); foreach( XmlNode authorNode in authorList ) {
6
10220
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 message: "The process cannot access the file "whatever" because it is being used by another process." I've even tried opening another file using the same streamwriter object before deleting the original file, but it's no use. Something keeps...
3
2038
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 propagating errors up the call stack? In the past, in my C++ code, I used to throw exceptions for ALL errors, exceptional or not. It seemed like a nice clean method for detecting, handling, and propagating errors. You can find multitudes of articles...
31
11431
by: Zytan | last post by:
There is no IsDisposed() method. I could just access it, and catch ObjectDisposedException, but that seems ugly. Zytan
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.