|
Hi everyone. My question is quite simple. I would like to know how exactly you would handle multiple types of exceptions in a method. Is this correct:
try
{
}
catch (FileNotFoundException fnf)
{
}
catch (ArgumentNullException ane)
{
}
catch (SomeException se)
{
}
I know it works. However, is that the best way to do it? I am asking because the exceptions that are thrown by different file operations, such as File.Open, are quite different from the types thrown by MessageBox.Show, for example. Or should I perform file operations and catch exceptions then perform other operations and catch their exceptions and so on?
Thanks in advance!
|
|
|
|
|
My approach has always been to do as much of mine own validation as possible. This prevents try/catch nesting that you've described.
For example, perform your own validation on the filepath and throw one ArgumentException:
try
{
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
throw new ArgumentException("A meaningful message goes here...")
}
catch (Exception ex)
{
The mind is like a parachute. It doesn’t work unless it’s open.
|
|
|
|
|
Alright, thanks for the timely response. I understand what you're saying. What about other operations that I can't check beforehand, such as UnauthorizedAccessException? If it can't be detected by something such as "string.IsNullOrEmpty(string)" or any other safety statement should I catch the appropriate exception then?
|
|
|
|
|
Hi,
IMO there is no best way, you should do whatever you think fits the functionality. Say it is all part of a method saving a user document, and all you want to obtain is, when things go wrong, you can tell the user the save operation failed and give some indication as to what caused that. Then you might do a simple catch(Exception exc) and communicate the problem.
OTOH if you're going to try and resolve several possible problems, each in their own way, you would need to have separate catch blocks. Whether they go with a common or several separate try blocks again is your choice.
BTW: Coincidentally I'm in the middle of writing a little article on exception handling right now!
|
|
|
|
|
Haha, I'll check the article out when it's up if you'll post a link.
I typically just want to tell the user that a failure occurred. But I also want to specify WHY it occurred. (eg. "You do not have permission to access this file.", from catching "UnauthorizedAccessException"). Should I avoid trying to provide the reason and just display a message that an error occurred? And maybe log the details to a file?
|
|
|
|
|
Matt U. wrote: "You do not have permission to access this file.", from catching "UnauthorizedAccessException
You do have one thing right. Always create user friendly error messages! Nothing annoys a user more than this message: "An error has occurred." Oh wow! What useful information! For all we know, a virus has just invaded our system.
If you handle most of the validation, you shouldn't have to worry about try/catch nesting because there won't be many more actions that can be executed.
Cheers!
Richard
The mind is like a parachute. It doesn’t work unless it’s open.
|
|
|
|
|
IMO you need to provide:
1. a user-friendly message that uses terms in the application domain (e.g. "save document has failed") so the user knows exactly what part of his actions did/did not succeed.
2. an indication of the kind of problem; this could be the Message part of the exception itself ("disk full").
3. if possible, a dialog that offers some choices, such as "try again", "choose another destination", ...
4. the full Exception.ToString() in a time-stamped log file (for administration/development purposes).
A useful feature is exception nesting: when a low-level exception is caugt, you could throw a higher-level exception (closer to the application domain) and include the low-level exception as a nested exception.
BTW: all my articles are reachable through the "My Articles" widget below.
|
|
|
|
|
Alright, thanks a lot. That helps me to understand the concept even more. I can't think of any other questions right now but if I do I'll be back. Thanks Luc and Richard.
|
|
|
|
|
Yes.
(Wrap the different operations in separate try/catch blocks. Perhaps in separate methods. But also do what you have there as necessary. And do any pre-validation you can.)
|
|
|
|
|
Matt U. wrote: However, is that the best way to do it?
I'd add a finally clause as a single placeholder for cleanup code.
/ravi
|
|
|
|
|
So I'm a programmer who's been programming for 20 years. I have written Windows services numerous times and need to do it again. There are things that my service code doesn't do (like run in console mode) that I'd like it to do.
So does anybody have any code that I can steal that is the basis for a Windows service? I know I can create a new project as a Windows Service through VS2010 but was wondering if anybody has a basis I can use.
Also - the premise I used was in the OnStart command, start a timer that would run a routine that would poll for things to do. The routine would never end and check, based on time, if something needs to be done. Is this still an appropriate method for doing a service or is there a better way?
TIA - Jeff.
|
|
|
|
|
Jeff - when I create a service, I tend to use a form of background thread to run the processing. If I have something that I need to run periodically, I use a Monitor because I use the Wait method for the period of time that I want the thread to sleep. This way, if I need to stop processing, I just Pulse the Monitor and shut it down.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I tend to write Services using a System.Timers.Timer -- so all I really need to do each time is write an Elapsed handler.
|
|
|
|
|
Hi all,
Please suggest me how to convert a word document to xsl/xslt (.doc file to xsl file). Please provide me some sample code or URL from where I can do this using C# 2.0 without using any third party tools.
Thanks in advance
Know is Drop, Unknown is Ocean
modified on Tuesday, July 20, 2010 9:16 AM
|
|
|
|
|
|
No, actually our requirement is to generate XSL file based on the word document (word document is an template with mail merge, later we merge data with this template document retrieved from database). We have different kind of document, for each selected document we have to create XSL file dynamically using C#.
Know is Drop, Unknown is Ocean
|
|
|
|
|
The I have no idea, but perhaps you could post a small example of what sort of structure is in the document and what XSL you want to produce.
|
|
|
|
|
Hi,
Thanks for you response.
Below is sample template document, in this Date, Name, Age, Address are merge field. I have to convert this template document to XLS.
<Date>
<Name>, <Age>
<Address>
Dear <Dr./Mr./Ms. Last Name>:
Below is the sample XLS for above template. (We created this manually)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format">
<xsl:template match="InvitationContemplateInfo">
<fo:block margin-left="0.785in" margin-top="0.7in" margin-right="0.88in" color="gray" font-family="'Times New Roman'" font-size="11.5pt" letter-spacing="0.15pt"><xsl:value-of select="//Date"/>
<xsl:if test="//Name[. != '']"> <xsl:value-of select="//Name"/></xsl:if>
<xsl:if test="//Age[. != '']"> <xsl:value-of select="//Age"/></xsl:if>
</fo:block>
</xsl:template>
</xsl:stylesheet>
Like this we have to create for all different kind of template documents dynamically.
Thank you once again.
Know is Drop, Unknown is Ocean
|
|
|
|
|
Have you tried the XML/XSL forum, you may have more luck there.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi People,
Let me be straight, I want to interact with a webserver from a DotNet Client. The web server is a Postgre Server and the server-side scripting language is PHP. The DotNet client is built in C#. I am actually new to this type of a concept but I was wondering whether it is actually possible or not. Its almost 5days now that I am not able to come up with any solid solution. I will be very much thankful if anyone can guide me out of this problem.
Thanks in advance
|
|
|
|
|
Assuming the server uses standard HTML to communicate then you should be able to access it through a client written in any language you like.
arj_agt wrote: Its almost 5days now that I am not able to come up with any solid solution.
A solution to what? Perhaps if you describe your problem someone may be able to offer some words of advice.
It's time for a new signature.
|
|
|
|
|
Thanks to all of you for your tips and advice. I have figured it out.
|
|
|
|
|
It's fairly simple. Server to Server communication is a Solved problem. I believe they came up with a human readable data interchange format to facilitate this sort of task in the late 90's. It's called XML and you can deliver it using either Restful calls or webservices and both languages can make and receive requests from each other across the Internet as well as share data in an interoperable format.
|
|
|
|
|
As mentioned, it would help if you described what the problem is.
Since your client and server are on 2 separate machines, if one sends information in a format the other can handle, it really doesn't matter what languages or operating systems are used on either end.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
HI ALL,
I am trying to use a dll made in C#.net(using framework 2.0) in to vb.net proj in framework 1.1 visual studio 2003
WHen I am trying to do add reference i am getting below error
"ADd reference to dll could not be added.This is not a valid assembly or COM component.ONly assemblies with extension 'dll' and COM components can be referenced.Please make sure that file is accessible,and that is valid assembly or COM component.
BUt when trying to add reference it in vs2005 proj in C#.net its successfully added.
ANy help will be greatly appreciated.
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
modified on Tuesday, July 20, 2010 9:34 AM
|
|
|
|