Throwing warnings from my code

I am writing a tool that processes some files. This tool will have a command line interface, but can also be used as a library of classes from third-party code. To deal with the error, I just throw an exception. Third-party code can handle the exception, and the command line interface can simply print it and abort. However, in addition to 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 can I deal with warnings so that both third-party code and the command line interface can do something with it?

+5
source share
2 answers

You also throw an exception for this (assuming that the error condition is really exceptional - it is expected that this will be rare).

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

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


, , , , - . .

+2

, , ( ). . .

+3

All Articles