Exception 构造函数 ()

这篇博客介绍了Exception类的无参数构造函数,用于初始化带有系统默认错误消息的新实例。文章强调所有派生类都应该提供这样的构造函数,并提供代码示例展示了如何使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.NET Framework 类库 

Exception 构造函数 ()

初始化 Exception 类的新实例。

[Visual Basic]
Public Sub New()
[C#]
public Exception();
[C++]
public: Exception();
[JScript]
public function Exception();
备注

此构造函数将新实例的 Message 属性初始化为系统提供的消息,该消息描述错误并考虑当前系统区域性。

所有派生类均应提供此默认构造函数。下表显示 Exception 实例的初始属性值。

属性Value
InnerException 空引用(在 Visual Basic 中为 Nothing)。
Message 系统提供的本地化说明。
示例

[Visual Basic, C#, C++] 以下代码示例派生一个使用预定义消息的 Exception。代码阐释了如何对派生类和 Exception 基类使用无参数构造函数。

[Visual Basic] 
' Example for the Exception( ) constructor.
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception with a predefined message.
    Class NotEvenException
        Inherits Exception
           
        Public Sub New( )
            MyBase.New( _
                "The argument to a function requiring " & _
                "even input is not divisible by 2." )
        End Sub ' New
    End Class ' NotEvenException

    Module NewExceptionDemo
       
        Sub Main( )
            Console.WriteLine( _
                "This example of the Exception( ) constructor " & _
                "generates the following output." )
            Console.WriteLine( vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of the base class." & _
                vbCrLf )

            CalcHalf( 12 )
            CalcHalf( 15 )
              
            Console.WriteLine(vbCrLf & _
                "Here, an exception is thrown using the " & vbCrLf & _
                "parameterless constructor of a derived class." & _
                vbCrLf )

            CalcHalf2( 24 )
            CalcHalf2( 27 )
        End Sub ' Main
           
        ' Half throws a base exception if the input is not even.
        Function Half( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New Exception( )
            Else
                Return input / 2
            End If
        End Function ' Half
            
        ' Half2 throws a derived exception if the input is not even.
        Function Half2( input As Integer ) As Integer

            If input Mod 2 <> 0 Then
                Throw New NotEvenException( )
            Else
                Return input / 2
            End If
        End Function ' Half2
            
        ' CalcHalf calls Half and catches any thrown exceptions.
        Sub CalcHalf( input As Integer )

            Try
                Dim halfInput As Integer = Half( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' CalcHalf
           
        ' CalcHalf2 calls Half2 and catches any thrown exceptions.
        Sub CalcHalf2( input As Integer )

            Try
                Dim halfInput As Integer = Half2( input )
                Console.WriteLine( _
                    "Half of {0} is {1}.", input, halfInput )

            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' CalcHalf2

    End Module ' NewExceptionDemo
End Namespace ' NDP_UE_VB

' This example of the Exception( ) constructor generates the following output.
' 
' Here, an exception is thrown using the
' parameterless constructor of the base class.
' 
' Half of 12 is 6.
' System.Exception: Exception of type System.Exception was thrown.
'    at NDP_UE_VB.NewExceptionDemo.Half(Int32 input)
'    at NDP_UE_VB.NewExceptionDemo.CalcHalf(Int32 input)
' 
' Here, an exception is thrown using the
' parameterless constructor of a derived class.
' 
' Half of 24 is 12.
' NDP_UE_VB.NotEvenException: The argument to a function requiring even input i
' s not divisible by 2.
'    at NDP_UE_VB.NewExceptionDemo.Half2(Int32 input)
'    at NDP_UE_VB.NewExceptionDemo.CalcHalf2(Int32 input)

[C#] 
// Example for the Exception( ) constructor.
using System;

namespace NDP_UE_CS
{
    // Derive an exception with a predefined message.
    class NotEvenException : Exception
    {
        public NotEvenException( ) :
            base( "The argument to a function requiring " +
                "even input is not divisible by 2." )
        { }
    }

    class NewExceptionDemo 
    {
        public static void Main() 
        {
            Console.WriteLine( 
                "This example of the Exception( ) constructor " +
                "generates the following output." );
            Console.WriteLine( 
                "/nHere, an exception is thrown using the /n" +
                "parameterless constructor of the base class./n" );

            CalcHalf( 12 );
            CalcHalf( 15 );

            Console.WriteLine( 
                "/nHere, an exception is thrown using the /n" +
                "parameterless constructor of a derived class./n" );

            CalcHalf2( 24 );
            CalcHalf2( 27 );
        }
                // Half throws a base exception if the input is not even.
        static int Half( int input )
        {
            if( input % 2 != 0 )
                throw new Exception( );

            else return input / 2;
        }

        // Half2 throws a derived exception if the input is not even.
        static int Half2( int input )
        {
            if( input % 2 != 0 )
                throw new NotEvenException( );

            else return input / 2;
        }

        // CalcHalf calls Half and catches any thrown exceptions.
        static void CalcHalf(int input )
        {
            try
            {
                int halfInput = Half( input );
                Console.WriteLine( 
                    "Half of {0} is {1}.", input, halfInput );
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }

        // CalcHalf2 calls Half2 and catches any thrown exceptions.
        static void CalcHalf2(int input )
        {
            try
            {
                int halfInput = Half2( input );
                Console.WriteLine( 
                    "Half of {0} is {1}.", input, halfInput );
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }
    }
}

/*
This example of the Exception( ) constructor generates the following output.

Here, an exception is thrown using the
parameterless constructor of the base class.

Half of 12 is 6.
System.Exception: Exception of type System.Exception was thrown.
   at NDP_UE_CS.NewExceptionDemo.Half(Int32 input)
   at NDP_UE_CS.NewExceptionDemo.CalcHalf(Int32 input)

Here, an exception is thrown using the
parameterless constructor of a derived class.

Half of 24 is 12.
NDP_UE_CS.NotEvenException: The argument to a function requiring even input is
not divisible by 2.
   at NDP_UE_CS.NewExceptionDemo.Half2(Int32 input)
   at NDP_UE_CS.NewExceptionDemo.CalcHalf2(Int32 input)
*/

[C++] 
// Example for the Exception( ) constructor.
#using <mscorlib.dll>
using namespace System;

namespace NDP_UE_CPP
{
    // Derive an exception with a predefined message.
    public __gc class NotEvenException : public Exception
    {
    public:
        NotEvenException( ) : Exception( 
            S"The argument to a function requiring "
            S"even input is not divisible by 2." )
        { }
    };

    // Half throws a base exception if the input is not even.
    int Half( int input )
    {
        if( input % 2 != 0 )
            throw new Exception( );

        else return input / 2;
    }

    // Half2 throws a derived exception if the input is not even.
    int Half2( int input )
    {
        if( input % 2 != 0 )
            throw new NotEvenException( );

        else return input / 2;
    }

    // CalcHalf calls Half and catches any thrown exceptions.
    void CalcHalf( int input )
    {
        try
        {
            int halfInput = Half( input );
            Console::WriteLine( S"Half of {0} is {1}.", 
                __box( input ), __box( halfInput ) );
        }
        catch( Exception* ex )
        {
            Console::WriteLine( ex->ToString( ) );
        }
    }

    // CalcHalf2 calls Half2 and catches any thrown exceptions.
    void CalcHalf2( int input )
    {
        try
        {
            int halfInput = Half2( input );
            Console::WriteLine( S"Half of {0} is {1}.", 
                __box( input ), __box( halfInput ) );
        }
        catch( Exception* ex )
        {
            Console::WriteLine( ex->ToString( ) );
        }
    }
}

void main() 
{
    Console::WriteLine( 
        S"This example of the Exception( ) constructor " 
        S"generates the following output." );
    Console::WriteLine( 
        S"/nHere, an exception is thrown using the /n" 
        S"parameterless constructor of the base class./n" );

    NDP_UE_CPP::CalcHalf( 12 );
    NDP_UE_CPP::CalcHalf( 15 );

    Console::WriteLine( 
        S"/nHere, an exception is thrown using the /n" 
        S"parameterless constructor of a derived class./n" );

    NDP_UE_CPP::CalcHalf2( 24 );
    NDP_UE_CPP::CalcHalf2( 27 );
}

/*
This example of the Exception( ) constructor generates the following output.

Here, an exception is thrown using the
parameterless constructor of the base class.

Half of 12 is 6.
System.Exception: Exception of type System.Exception was thrown.
   at NDP_UE_CPP.Half(Int32 input)
   at NDP_UE_CPP.CalcHalf(Int32 input)

Here, an exception is thrown using the
parameterless constructor of a derived class.

Half of 24 is 12.
NDP_UE_CPP.NotEvenException: The argument to a function requiring even input is
 not divisible by 2.
   at NDP_UE_CPP.Half2(Int32 input)
   at NDP_UE_CPP.CalcHalf2(Int32 input)
*/
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值