CodeCatchClause Klasa

Definicja

catch Reprezentuje blok wyjątku instrukcjitry/catch.

C#
public class CodeCatchClause
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeCatchClause
Dziedziczenie
CodeCatchClause
Atrybuty

Przykłady

Poniższy przykładowy kod demonstruje używanie CodeCatchClause obiektów do definiowania klauzul obsługi wyjątków try... catch block.

C#
// 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");
//        }
//    }

Uwagi

CodeCatchClause może służyć do reprezentowania catch bloku wyjątków instrukcji try/catch .

Właściwość CatchExceptionType określa typ wyjątku do przechwycenia. Właściwość LocalName określa nazwę zmiennej reprezentującej wyjątek, który został przechwycony. Właściwość Statements collection zawiera instrukcje dla catch bloku.

Konstruktory

CodeCatchClause()

Inicjuje nowe wystąpienie klasy CodeCatchClause.

CodeCatchClause(String)

Inicjuje nowe wystąpienie CodeCatchClause klasy przy użyciu określonej nazwy zmiennej lokalnej dla wyjątku.

CodeCatchClause(String, CodeTypeReference)

Inicjuje nowe wystąpienie CodeCatchClause klasy przy użyciu określonej nazwy zmiennej lokalnej dla wyjątku i typu wyjątku.

CodeCatchClause(String, CodeTypeReference, CodeStatement[])

Inicjuje nowe wystąpienie CodeCatchClause klasy przy użyciu określonej lokalnej nazwy zmiennej dla wyjątku, typu wyjątku i kolekcji instrukcji.

Właściwości

CatchExceptionType

Pobiera lub ustawia typ wyjątku do obsługi z blokiem catch.

LocalName

Pobiera lub ustawia nazwę zmiennej wyjątku obsługiwanego przez klauzulę catch .

Statements

Pobiera instrukcje w bloku catch.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Produkt Wersje
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Zobacz też