It seems you need a composite validator in which your users can connect to their own logic to designate specific non-fatal errors as fatal, warning or informational messages. Throwing exceptions does not match the bill, because you already left this method if the other part wanted to turn it into a warning, but wanted to continue parsing. This sounds the same as the two exception skipping models for skipping in Windows to handle structured exceptions. It basically looks like
- Register as many exception handlers as you like
- When a problem is detected in the passage, all the handlers are set (until the exploit is thrown!), Which must be processed with an error. The first, which says yes, will become the actual handler that decides what to do.
- When a handler that can handle it was found, we get 2 and call it. This time this is the exception - throw time or not. It is entirely up to the handler.
The beauty of this two-pass model is that during the first pass all handlers are set, but you can still continue parsing when you are gone. Not a single state has yet been destroyed.
In C # you, of course, have much more freedom to turn this into a more flexible error conversion and report pipeline, where you can convert for a strict reader, for example. all error warnings.
Which route you need depends on how your library is used, and how qualified the users of your library are and how many service requests you want to handle, as some users make dumb mistakes. There is always a trade-off between beeing as strict as possible (perhaps too strict and you get a complicated library) or too relaxed (a pretty parser, but silently skips half of my input due to internal errors).
The Windows SDK libraries are sometimes quite difficult to use because the engineers there optimize more for less ringing calls. They throw a Win32 or HResult error code at you, and you must find out which principle (memory alignment, buffer size, cross-threading ...) you violated.
source share