473,466 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Use C# method attributes for standard exception handling

Hi,

I have been reading a bit basic stuff about method attributes in C#,
and I wanted to build the following functionality with it:

if a method has the custom attribute <LogException> the method has to
be wrapped as follows:

try
{
// call method
}
catch(System.Exception ex)
{
SaveToDb(ex);
throw ex;
}

Can somebody help me on the way how to implement this kind of
functionality? I'm quite sure this is possible, since i assume the
microsoft exception framework uses the same technique.

Tnx for any help.

Regards,

Koen Appeltans

Apr 24 '06 #1
3 7757
Koen,

I don't quite understand what are you trying to accomplish.
Are you trying to force programmers using this method to put it in a
try/catch or you want the compiler to generate this code for you. Either
ways I don't think you can do it via attributes.
What you can do though, is to add try/catch in your code.
for example:

class Foo
{
public Bar()
{
try
{
BarImp();
}
catch(System.Exception ex)
{
SaveToDb(ex);
throw ex;
}
}

private BarImp()
{
// method implementation.
}
}

The user see Bar method and calling it will log any exception if it happens.
Ofcourse you don't nee to split the implementaiton on Bar and BarImp both
can go in the same method.

Anyways I'd suggest to check out the logging frameworks that are out there
or look at the System.Diagnostics names space and find out how to use
listeners for implementing more flexible logging system.
--

Stoitcho Goutsev (100).
"Koen" <de******@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi,

I have been reading a bit basic stuff about method attributes in C#,
and I wanted to build the following functionality with it:

if a method has the custom attribute <LogException> the method has to
be wrapped as follows:

try
{
// call method
}
catch(System.Exception ex)
{
SaveToDb(ex);
throw ex;
}

Can somebody help me on the way how to implement this kind of
functionality? I'm quite sure this is possible, since i assume the
microsoft exception framework uses the same technique.

Tnx for any help.

Regards,

Koen Appeltans

Apr 24 '06 #2
I think that what the OP is after is Java-style forced exception
handling. If that's the case, then no, there is no way to do that in
C#. Not even with exceptions, and no, the compiler doesn't use a
similar mechanism for anything.

Apr 24 '06 #3
Hi Stoitcho,

tnx for the response.

your implementation is the way it is done now.
since i'm a bit of a lazy coder, i was wondering if that try catch
statement could be done by an attribute.
now i guess i understood attributes a bit wrong, i thought you could
add code in the beginning and the end of your method with attributes,
but they're meant as some information about the method, a
characteristic.

so i guess i'll simply continue to write the exception code as i did in
the past.

regards,

Koen

Stoitcho Goutsev (100) schreef:
Koen,

I don't quite understand what are you trying to accomplish.
Are you trying to force programmers using this method to put it in a
try/catch or you want the compiler to generate this code for you. Either
ways I don't think you can do it via attributes.
What you can do though, is to add try/catch in your code.
for example:

class Foo
{
public Bar()
{
try
{
BarImp();
}
catch(System.Exception ex)
{
SaveToDb(ex);
throw ex;
}
}

private BarImp()
{
// method implementation.
}
}

The user see Bar method and calling it will log any exception if it happens.
Ofcourse you don't nee to split the implementaiton on Bar and BarImp both
can go in the same method.

Anyways I'd suggest to check out the logging frameworks that are out there
or look at the System.Diagnostics names space and find out how to use
listeners for implementing more flexible logging system.
--

Stoitcho Goutsev (100).
"Koen" <de******@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi,

I have been reading a bit basic stuff about method attributes in C#,
and I wanted to build the following functionality with it:

if a method has the custom attribute <LogException> the method has to
be wrapped as follows:

try
{
// call method
}
catch(System.Exception ex)
{
SaveToDb(ex);
throw ex;
}

Can somebody help me on the way how to implement this kind of
functionality? I'm quite sure this is possible, since i assume the
microsoft exception framework uses the same technique.

Tnx for any help.

Regards,

Koen Appeltans


Apr 25 '06 #4

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

Similar topics

5
by: Max Ischenko | last post by:
Hi, I wrote simple implementation of the "synchronized" methods (a-la Java), could you please check if it is OK: def synchronized(method): """ Guards method execution, similar to Java's...
12
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something...
31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
16
by: Levin Alexander | last post by:
Hi, (I hope that this is not a FAQ) I'm trying to make a timer call a method of an object. void MyClass::myCallback(int par) {cout << "Callback Called" << std::endl;} void...
3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
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
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...
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...
1
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...
0
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,...
0
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
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.