Geeks With Blogs

News
Charles Young

A quick tip.  The Throw shape in BizTalk can cause a little initial confusion.  The shape has an 'Exception Object' property which you assign using a drop-down list.   When you first drag the shape onto your Orchestration, the only item in the list is 'General Exception'.  Intuitively, you might expect that you can select an exception class, in a similar fashion to setting the 'Exception Object' property of a Catch Exception block.   However, this is not the case.   If you wish to throw a specific exception, you will need first to create a variable of an exception type (or use the exception object defined in a Catch Exception block).   This variable will then be displayed in the drop-down list of the 'Exception Object' property.  There is no need to write code to instantiate the object, as it will be automatically instantiated.   However, you may wish to include expression code to set property values.

You can only use 'General Exception' if the Throw statement is placed in a Catch Exception block - i.e., you are rethrowing an exception.  I did some testing, and it appears that this is essentially the meaning of setting 'General Exception' on a Throw shape.  It rethrows the existing exception object, as is.  The exception is raised by calling an inherited method of the BizTalk Context class.  

When catching exceptions, choosing 'General Exception' as the Exception Object allows Catch Exception blocks to handle exceptions which are not derived from System.Exception. The .NET framework allows any class to be raised as an exception, but some .NET languages such as C# impose a tighter restriction that exceptions must be System.Exception classes.   'General Exception' allows BizTalk to deal with any exception it may catch (and rethrow) from an external .NET component, regardless of the language that component was written in.

[UPDATE] In most scenarios, the use of General Exception in a catch block is now redundant in BTS 2006 and later.   Since CLR 2.0, whenever an exception is thrown, and that exception is not derived from System.Exception, the CLR automatically wraps the exception in an instance of the System.Runtime.CompilerServices.RuntimWrappedException class.   By default, the C# compiler, used by BizTalk to compile orchestration code, is compiled with the following Assembly attribute:

[assembly: System.Runtime.CompilerServicesRuntimeCompatibility(WrapNonExceptionThrows=true)]

The parameter name is slightly misleading.   When set to true, C# does not automatically unwrap instances of RuntimWrappedException.   When set to false, it does.

In BizTalk Server 2009, it is simple to manually set WrapNonExceptionThrows to false in the Assembly code file.   In this case, non-System Exceptions could be caught by General Exception.   However, in normal circumstances, all exceptions will be caught as System.Exceptions.   The only remaining use of a general exception catch block is as the equivalent of a 'catch' in C# without an explicit exception type.

Posted on Monday, April 26, 2004 10:24 AM BizTalk Server 2004/2006 | Back to top


Comments on this post: The BizTalk Server 2004 Throw shape and 'General Exception'

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
Charles,

Suppose you have a Scope shape with an Exception Handler block which catches a General Exception. As you described in your post, this will catch all exceptions that can happen in the Scope. The downside of using the General Exception is that you don't know what went wrong, there is no exception object, so you can get a hold of the error message. So what I would like to do is to use a General Exception handler, but inside the catch block I want to be able to get the error message. Any ideas how to do that?

Jan
Left by Jan on Apr 26, 2004 10:44 AM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
A good point. I don't believe you can do this because BizTalk does not expose the object to your XLANG code. Indeed, from what I have seen, the exception object, in this case, is kept at arms length from the Orchestration code. I think the idea is to avoid catching a 'General Exception' unless you really need to. I would just use System.Exception or a derived class, as this will always work if you are using external C# code (and VB.NET code as well, I believe).

Throwing a 'General Exception' is a different matter. BizTalk just throws the existing exception object, which you can then catch using an explicit exception handler in the normal way.
Left by Charles Young on Apr 26, 2004 10:51 AM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
Sure the System.Exception would suffice for C# or VB.NET exceptions. But when thing really go wrong BizTalk throws an exception that's not inherited from the System.Exception class so the catch won't catch it. :-)
Left by Jan on Apr 26, 2004 2:27 PM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
Is this behaviour that you have seen? I.e., Does the BTS engine throw non-System.Exception objects? Or is this just 'hypothetical'. I haven't coded with C++ managed extensions, for example, but I suspect you could probably throw a non-System.Exception from a C++ component.
Left by Charles Young on Apr 26, 2004 2:53 PM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
You probably already have a solution to your problem but anyway.. I just ran into the 'illegal name-hiding' problem myself when catching a system.Exception in an orchestration. The Property "Exception Object Name" was set to the same name as my Exception variable. Changing that name to something else and refering to the new name in the catch expression solved the problem.
Left by Peter on Mar 09, 2005 10:58 AM

# Handling Exceptions within an Orchestration
Requesting Gravatar...
BizTalk Server 2004 allows you to catch an exception within an orchestration through the Scope shape....
Left by { dispatches } on Nov 04, 2005 12:13 PM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
In BTS 2006, I am trying to throw a System.Exception with a specific message. However, when I try to assign the message property, I get an error: "Cannot modify a readonly or literal field." Also, the message variable does not appear as an Exception Obejct type in my Throw Exception shape. Any ideas?
Left by Daniel Toomey on Sep 10, 2006 11:31 PM

# re: The BizTalk Server 2004 Throw shape and 'General Exception'
Requesting Gravatar...
Sure the System.Exception would suffice for C# or VB.NET exceptions. But when thing really go wrong BizTalk throws an exception that's not inherited from the System.
Left by savaş oyunu on Dec 27, 2008 11:32 PM

Your comment:
 (will show your gravatar)


Copyright © Charles Young | Powered by: GeeksWithBlogs.net