CodeSmith: Custom Exception Template v1.1

I recently picked up a license for CodeSmith Professional and upgraded from version 2.6 to the brand new version 3.1.  As a long-time user of CodeSmith, I love the new enhancements to both the core engine and the Studio!   The built-in Statement Completion (e.g. intellisense-style lookup) feature is worth it alone!  Great work Eric!

If you arent doing code-generation or arent familiar with CodeSmith, you should RUN, not walk, to your PC and visit CodeSmithTools.com and install the 30-day trial version!

I can't wait to try out the improved code-behind support as well as the support for nesting templates.  I had attempted to do this last year with some custom ORM generator templates, but couldnt quite get it working.  Maybe this new version will be the ticket.    The only thing that is still missing in the new version is direct support for multiple output-files from a single template.   However, with the ability to override the Render() method in CodeSmith v3.x that can even be overcome with a bit of custom plumbing for the output.

Eric mentioned at the last North Dallas .NET User Group meeting that there might be one or two obscure breaking changes in this version, so I decided to retest my custom templates.  It turns out that I had a poorly placed comment-block in my CustomException template that caused errors during compile.    No big deal - it took exactly 5 seconds to fix within the Studio.

I removed the old template, but you can download the latest version of my CustomException template here.

--------

What is this CustomException template?

In a nutshell the CustomException template generates an exception class for C# that conforms to many of the recommended best practices advocated on MSDN such as derriving from System.Exception and providing the neccessary overrides for proper serialization.  Also, for those writing COM Callable Wrappers (CCW), it defaults the HResult to the value for COR_ApplicationException, but you can override this by changing the HResult parameter value.  This can be helpful for ensuring your exceptions are marshalled back to the appropriate COM exception.  Undoubtedly this template can be improved, so feel free to post your comments here, or on the CodeSmith forums.

No Comments