473,513 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Repetivie Try/Catch Blocks

I have a couple of standard Try/Catch blocks that I use repeatedly. Is there
anyway to wrap these blocks into a method call? Or does C# have anything like a
C++ macro?

Thanks,
Steve Murphy
Nov 16 '05 #1
5 2374
Steve,

Unfortunately, no, there isn't anything like a macro.

In VS.NET 2005 though, you can have code snippets, meaning that when you
type a certain phrase, you can have it extend into a larger block of code.

Check out the section on MSDN titled "Investigating Code Snippet
Technology", located at (watch for line wrap):

http://msdn.microsoft.com/library/de...desnippets.asp

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Steve Murphy" <sm*****@granite-ridge.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have a couple of standard Try/Catch blocks that I use repeatedly. Is
there
anyway to wrap these blocks into a method call? Or does C# have anything
like a
C++ macro?

Thanks,
Steve Murphy

Nov 16 '05 #2
"Steve Murphy" <sm*****@granite-ridge.com> wrote:
I have a couple of standard Try/Catch blocks that I
use repeatedly. Is there anyway to wrap these blocks
into a method call?


Here is one possibility, using a delegate (which you could modify to
return a value). Personally, I wouldn't use this approach - it seems a
bit clumsy and long-winded - but I can't think of an alternative.

private delegate void GenericDelegate();

static void Main(string[] args)
{
RunWithHandler(new GenericDelegate(DoDivision));
}

private static void DoDivision()
{
int i = 0;
Console.WriteLine(i / i); // this will fail
}

private static void RunWithHandler(GenericDelegate theCode)
{
try
{
theCode();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

P.
Nov 16 '05 #3
clu
Oh ... the missing macros !

Take a look at this post and to its comments:
http://blogs.msdn.com/ericgu/archive...31/363861.aspx

Claudio

Nov 16 '05 #4
"clu" <cl************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Oh ... the missing macros !

Take a look at this post and to its comments:
http://blogs.msdn.com/ericgu/archive...31/363861.aspx


I'd agree with the author... you can get into a right old mess with macros
in c++...
Nov 16 '05 #5
Steve Murphy wrote:
I have a couple of standard Try/Catch blocks that I use repeatedly. Is there
anyway to wrap these blocks into a method call? Or does C# have anything like a
C++ macro?


As suggested by others, you can use delegates combined with functions,
but that has the disadvangate of Exception wrapping:
delegate-invocations doesn't directly throw exceptions, but throws an
exception with the original exception as it's .Inner. This is
understandable with async execution of delegates but I cannot find any
reason for the behaciour with sync. invoked delegates.

Also the delegate-approach requires a delegate for every signarure of
the code to exec, making for almost as big a mess as copying try...catch.

You could consider whether your prorgam could be implemented with less
catch-clauses. I usually advice to catch only when one of:

- You can fix the situation: try another way, ...
- You are at the top of the stack

A special exception from this is catch for logging:

try {
...
} catch ( Exception e ) {
Logging.Log(e);
throw;
}

If you find that the catch-logic is actually "finally" logic, and there
is a 1-1 relation between the code to exec and the error, you can use
the "using" pattern for cleanup.

Finally, if all else fail, you can try to factorize the body of the
catch-handler into a function so you wont have to copy all the
exception-handling code into every catch-block.

--
Helge
Nov 16 '05 #6

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

Similar topics

6
552
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
4
1500
by: B.J. | last post by:
Hi, Is there any way how I can shorten following code ? try {...} catch(FormatException) {SAME CODE IN ALL CATCH BLOCKS} catch(OverflowException) {SAME CODE IN ALL CATCH BLOCKS} E.g. to...
4
9614
by: Chris Martin | last post by:
Below are three try-catch blocks. My question is, how can all three work, and how come there is no memory leak (or is there)? As I understand it, an exception object is being created and "thrown"...
6
1897
by: George | last post by:
Hi All, I thought this would not compile because no return value is specified. But it does compile and run (aix and xlc v7.0.) Could someone kindly please point me to where in the spec this...
4
3123
by: Big D | last post by:
Hi all, I'm trying to understand the best way to use a try catch with a SQL transaction. I have a number of sql statements that need to be run, such as: 'open sqlConnection, get command...
8
2721
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
11
3460
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
3
1284
by: stktung | last post by:
Hi, I'm not too familiar with how exception works in .NET but I've read from somewhere that in C++, code that are ran in try blocks are expensive: ...
0
7260
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
7384
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
7539
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...
1
7101
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
7527
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...
0
5686
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,...
1
5090
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...
0
4746
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...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.