Export (0) Print
Expand All

CodeCatchClause Class

Represents a catch exception block of a try/catch statement.

Namespace: System.CodeDom
Assembly: System (in system.dll)

[SerializableAttribute] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class CodeCatchClause
/** @attribute SerializableAttribute() */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class CodeCatchClause
SerializableAttribute 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class CodeCatchClause
Not applicable.

CodeCatchClause can be used to represent a catch exception block of a try/catch statement.

The CatchExceptionType property specifies the type of exception to catch. The LocalName property specifies a name for the variable representing the exception that has been caught. The Statements collection property contains the statements for the catch block.

The following example code demonstrates using CodeCatchClause objects to define exception handling clauses of a try...catch block.

// Declares a type to contain a try...catch block.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("TryCatchTest");

// Defines a method that throws an exception of type System.ApplicationException.
CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "ThrowApplicationException";
method1.Statements.Add( new CodeThrowExceptionStatement( 
    new CodeObjectCreateExpression("System.ApplicationException", new CodePrimitiveExpression("Test Application Exception")) ) );
type1.Members.Add( method1 );

// Defines a constructor that calls the ThrowApplicationException method from a try block.
CodeConstructor constructor1 = new CodeConstructor();
constructor1.Attributes = MemberAttributes.Public;            
type1.Members.Add( constructor1 );    
        
// Defines a try statement that calls the ThrowApplicationException method.
CodeTryCatchFinallyStatement try1 = new CodeTryCatchFinallyStatement();
try1.TryStatements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "ThrowApplicationException" ) );
constructor1.Statements.Add( try1 );                    

// Defines a catch clause for exceptions of type ApplicationException.
CodeCatchClause catch1 = new CodeCatchClause("ex", new CodeTypeReference("System.ApplicationException"));
catch1.Statements.Add( new CodeCommentStatement("Handle any System.ApplicationException here.") );
try1.CatchClauses.Add( catch1 );

// Defines a catch clause for any remaining unhandled exception types.
CodeCatchClause catch2 = new CodeCatchClause("ex");
catch2.Statements.Add( new CodeCommentStatement("Handle any other exception type here.") );
try1.CatchClauses.Add( catch2 );
        
// Defines a finally block by adding to the FinallyStatements collection.
try1.FinallyStatements.Add( new CodeCommentStatement("Handle any finally block statements.") );
        
// A C# code generator produces the following source code for the preceeding example code:
    
//    public class TryCatchTest 
//    {
//        
//        public TryCatchTest() 
//        {
//            try 
//            {
//                this.ThrowApplicationException();
//            }
//            catch (System.ApplicationException ex) 
//            {
//                // Handle any System.ApplicationException here.
//            }
//            catch (System.Exception ex) 
//            {
//                // Handle any other exception type here.
//            }
//          finally {
//                // Handle any finally block statements.
//            }
//        }
//        
//        private void ThrowApplicationException() 
//        {
//            throw new System.ApplicationException("Test Application Exception");
//        }
//    }


// Declares a type to contain a try...catch block.
CodeTypeDeclaration type1 = new CodeTypeDeclaration("TryCatchTest");
// Defines a method that throws an exception of type
// System.ApplicationException.
CodeMemberMethod method1 = new CodeMemberMethod();
method1.set_Name("ThrowApplicationException");
method1.get_Statements().Add(new 
    CodeThrowExceptionStatement(new CodeObjectCreateExpression(
    "System.ApplicationException", new CodeExpression[] { 
    new CodePrimitiveExpression("Test Application Exception") })));
type1.get_Members().Add(method1);
// Defines a constructor that calls the ThrowApplicationException 
// method from a try block.
CodeConstructor constructor1 = new CodeConstructor();
constructor1.set_Attributes(MemberAttributes.Public);
type1.get_Members().Add(constructor1);
// Defines a try statement that calls the 
// ThrowApplicationException method.
CodeTryCatchFinallyStatement try1 = new CodeTryCatchFinallyStatement();
try1.get_TryStatements().Add(new CodeMethodInvokeExpression(new 
    CodeThisReferenceExpression(), "ThrowApplicationException", 
    new CodeExpression[] {}));
constructor1.get_Statements().Add(try1);
// Defines a catch clause for exceptions of type ApplicationException.
CodeCatchClause catch1 = new CodeCatchClause("ex", new 
    CodeTypeReference("System.ApplicationException"));
catch1.get_Statements().Add(new CodeCommentStatement(
    "Handle any System.ApplicationException here."));
try1.get_CatchClauses().Add(catch1);
// Defines a catch clause for any remaining unhandled exception types.
CodeCatchClause catch2 = new CodeCatchClause("ex");
catch2.get_Statements().Add(new CodeCommentStatement(
    "Handle any other exception type here."));
try1.get_CatchClauses().Add(catch2);
// Defines a finally block by adding to the FinallyStatements collection.
try1.get_FinallyStatements().Add(new CodeCommentStatement(
    "Handle any finally block statements."));
// A VJ# code generator produces the following source code for the 
// preceeding example code:
//    public class TryCatchTest 
//    {
//        
//        public TryCatchTest() 
//        {
//            try {
//                this.ThrowApplicationException();
//            }
//            catch (System.ApplicationException ex) {
//                    // Handle any System.ApplicationException here.
//            }
//            catch (System.Exception ex) {
//                // Handle any other exception type here.
//            }
//          finally {
//                // Handle any finally block statements.
//            }
//        }
//        
//        private void ThrowApplicationException() 
//        {
//            throw new System.ApplicationException(
//                "Test Application Exception");
//        }
//    }

System.Object
  System.CodeDom.CodeCatchClause

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft