Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Throwing warnings' from my code

Tags:

c#

warnings

I am writing a tool that processes some files. This tool will have a command-line interface but can also be used as a class library from third-party code. To deal with an error, I just throw an exception. Third-party code can handle the exception, and the command-line interface might just print it and abort. However, apart from fatal errors, it is also possible that a situation arises that is not fatal and the process can continue, for which I would like to 'throw a warning' and continue.

How do I deal with warnings, in such way that both third-party code and the command-line interface can do something with it?

like image 587
Daniel A.A. Pelsmaeker Avatar asked Sep 15 '12 18:09

Daniel A.A. Pelsmaeker


2 Answers

I would suggest you not to throw an exception, they should be avoided if possible (they are resources costly). Instead you can create an event and raise it. Third party code and command-line interface will just need to sign to this event.

like image 56
Gregor Primar Avatar answered Oct 18 '22 06:10

Gregor Primar


You throw an exception for this as well (assuming the error condition is indeed exceptional - that is expected to be rare).

Exceptions are not always lethal - you need to throw a specific type of exception that the third party code and command line code can catch and act on.

One rule of exception handling is to handle exceptions you know how to handle - if you have a specific exception type for the error and you document it, client code should know how to deal with it (or not).


If what you are trying to do is provide information to using code - warnings that are handled within your library already but that the users may want to know - you can output traces using the tracing subsystem. All users will need to do is configure a listener and will be able to get the information during runtime.

like image 35
Oded Avatar answered Oct 18 '22 05:10

Oded