Lance's Whiteboard

Random scribbling about C#, Javascript, Web Development, Architecture, and anything else that pops into my mind.

News


Creative Commons License
Lance's Whiteboard Blog by Lance Hunt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Based on a work at weblogs.asp.net



Sponsored Ad
Sponsored Ad

Blogs I Read

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.

Comments

css said:

# August 30, 2005 11:40 AM

css said:

Answered my own question.
-2146232832 = 0x80131600 which is the hresult for System.ApplicationException

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemapplicationexceptionclasstopic.asp

So you should probably change the hresult to the hresult for System.Exception or derive from System.ApplicationException instead since you are using that hresult already.
# August 30, 2005 11:46 AM

css said:

This inherit from Exception or ApplicationException debate is interesting:
http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx

So maybe you were right in the first place :).
# August 30, 2005 12:00 PM

Lance's Whiteboard said:

I justposted an update to my CodeSmith Exception Template with some minor changes to formatting, the

# August 28, 2006 11:43 AM

Lance's Whiteboard said:

Codesmith_CustomExceptionTemplateV1.4.zip (C# only)Version 1.4 Scenario/Problem: "I need to implement

# September 28, 2006 12:49 PM