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

C# How to Handle Exception Smartly.???

Hi Pals,

Application main(), handles the default Exception.
However at Method(function) level i want to catch certain exceptions.
certain method X(), using all System.IO.Path,System.IO.Directory etc objects.
Is it wise to catch all exception thrown by these classes/methods.
Then my method looks like.

void X()
{
try {
....
}
catch(IOException ex)
catch(UnauthorizedAccessException ex)
catch(ArgumentException ex)
catch(ArgumentNullException ex)
catch(PathTooLongException ex)
catch(DirectoryNotFoundException ex)
catch(NotSupportedException ex)


}

Im looking at the way how can i organize the exception handling in method X().
using excessive Catch block do not want to exploit my code.
Nov 16 '07 #1
3 1915
Plater
7,872 Expert 4TB
I'm not sure what you're asking but...
You will always want to catch exceptions, however sometimes Exceptions you catch aren't a "big deal".
Like with HttpWebRequest, if you get 404 or other bad status message an exception is thrown. You will want to trap it, but don't need to do anything with it specifically.

The Exception base class will catch everything.

Consider:
Expand|Select|Wrap|Line Numbers
  1. catch (Exception ee)
  2. {
  3.    if (ee.GetType()==typeof(NullReferenceException))
  4.    {
  5.       //do something
  6.    }
  7.    else
  8.    {
  9.       //do nothing
  10.    }
  11. }
  12.  
is the same as:
Expand|Select|Wrap|Line Numbers
  1. catch(NullReferenceException nre)
  2. {
  3.    //do something
  4. }
  5. catch(Exception ee)
  6. {
  7.    //do nothing
  8. }
  9.  
Nov 16 '07 #2
Thanks Pal.

I wanted no to have many catch clause in my code.
So i have to fall back for Exception generic class.
catch(Exception ex)
{
// use ex.Message....
}
Nov 19 '07 #3
r035198x
13,262 8TB
Thanks Pal.

I wanted no to have many catch clause in my code.
So i have to fall back for Exception generic class.
catch(Exception ex)
{
// use ex.Message....
}
Well in that case then that is not the "smart" way of handling exceptions.
If you know the actual(exact) expection that can be throw, then catch it separately and handle it in a manner specific only to that exception.
e.g If the exception is security related, then hadle it differently from how you would handle incorrect input from the user.
Nov 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Thomas Miller | last post by:
I am deploying some software. I want the installer (wise, installshield, or whatever you reccommend) to smartly analyze the users computer and look to see if the .net framework is installed. If it...
0
by: Matt Warner | last post by:
Hi guys, A couple of people have already posted questions about similar issues but haven't had any response. Occasionally, sometimes after running the app for a few hours, it bombs out saying...
17
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment...
8
by: Shawn B. | last post by:
Greetings, Me again. I have (roughly) the following code: HANDLE hConsoleOutput; HANDLE hConsoleInput;
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...
7
by: Ryan | last post by:
I'm creating a user control that has a handful of controls on it (19 in total). One of the controls on the UC is a textbox for a user's password, so I've set the PasswordChar property to "*". I...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: =?Utf-8?B?RGFya3Jpc2Vy?= | last post by:
I have managed code in C# calling unmanaged code from a DLL written in C++. Every time an Exception is thrown by unmanaged code, this exception is handled by C# directly. How to force to handle...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.