EncoderExceptionFallback 클래스

정의

출력 바이트 시퀀스로 변환할 수 없는 입력 문자에 대체(fallback)라고 하는 오류 처리 메커니즘을 제공합니다. 입력 문자를 출력 바이트 시퀀스로 변환할 수 없으면 대체(fallback)는 예외를 throw합니다. 이 클래스는 상속될 수 없습니다.

public ref class EncoderExceptionFallback sealed : System::Text::EncoderFallback
public sealed class EncoderExceptionFallback : System.Text.EncoderFallback
[System.Serializable]
public sealed class EncoderExceptionFallback : System.Text.EncoderFallback
type EncoderExceptionFallback = class
    inherit EncoderFallback
[<System.Serializable>]
type EncoderExceptionFallback = class
    inherit EncoderFallback
Public NotInheritable Class EncoderExceptionFallback
Inherits EncoderFallback
상속
EncoderExceptionFallback
특성

예제

다음 코드 예제에서는 클래스 및 EncoderFallbackException 클래스를 EncoderExceptionFallback 보여 줍니다.

// This example demonstrates the EncoderExceptionFallback class.

using namespace System;
using namespace System::Text;

int main()
{
    // Create an encoding, which is equivalent to calling the
    // ASCIIEncoding class constructor.
    // The EncoderExceptionFallback parameter causes an exception to
    // be thrown when a character cannot be encoded.
    // A decoder exception fallback is also specified, but it is not
    // used because this example terminates during the encoding operation.

    Encoding^ asciiEncoding = Encoding::GetEncoding("us-ascii",
        gcnew EncoderExceptionFallback(), gcnew DecoderExceptionFallback());

    // The input string consists of the Unicode characters LEFT POINTING
    // DOUBLE ANGLE QUOTATION MARK (U+00AB), 'X' (U+0058), and RIGHT
    // POINTING DOUBLE ANGLE QUOTATION MARK (U+00BB).
    // The encoding can only encode characters in the US-ASCII range of
    // U+0000 through U+007F. Consequently, the characters bracketing the
    // 'X' character cause an exception.
    String^ inputString = L"\u00abX\u00bb";

    String^ twoNewLines = Environment::NewLine + Environment::NewLine;
    array<Byte>^ encodedBytes = gcnew array<Byte>(
        asciiEncoding->GetMaxByteCount(inputString->Length));
    int numberOfEncodedBytes = 0;

    // ---------------------------------------------------------------------
    Console::Clear();

    // Display the name of the encoding.
    Console::WriteLine("The name of the encoding is \"{0}\".{1}",
        asciiEncoding->WebName, Environment::NewLine);

    // Display the input string in text.
    Console::WriteLine("Input string ({0} characters): \"{1}\"",
        inputString->Length, inputString);

    // Display the input string in hexadecimal.
    Console::Write("Input string in hexadecimal: ");
    for each (char c in inputString)
    {
        Console::Write("0x{0:X2} ", c);
    }
    Console::Write(twoNewLines);

    // ---------------------------------------------------------------------
    // Attempt to encode the input string. However, an exception is thrown
    // before the input string can be encoded.

    Console::WriteLine("Encode the input string...");

    // The code example terminates during the call to the GetBytes() method.
    try
    {
        numberOfEncodedBytes = asciiEncoding->GetBytes(inputString, 0,
            inputString->Length, encodedBytes, 0);
        // This statement is never executed.
        Console::WriteLine("This statement is never executed.");
    }
    catch (EncoderFallbackException^ ex)
    {
        Console::WriteLine(ex);
        Console::WriteLine(
            "{0}*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***",
            Environment::NewLine);
    }
}

/*
This code example produces the following results:

The name of the encoding is "us-ascii".

Input string (3 characters): "X"
Input string in hexadecimal: 0xAB 0x58 0xBB

Encode the input string...
System.Text.EncoderFallbackException: Unable to translate Unicode character \u00AB at inde
x 0 to specified code page.
at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
at System.Text.ASCIIEncoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32
byteCount, EncoderNLS encoder)
at System.Text.ASCIIEncoding.GetBytes(String chars, Int32 charIndex, Int32 charCount, B
yte[] bytes, Int32 byteIndex)
at Sample.Main()

*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***

*/
// This example demonstrates the EncoderExceptionFallback class.

using System;
using System.Text;

class Sample
{
    public static void Main()
    {

// Create an encoding, which is equivalent to calling the
// ASCIIEncoding class constructor.
// The EncoderExceptionFallback parameter causes an exception to
// be thrown when a character cannot be encoded.
// A decoder exception fallback is also specified, but it is not
// used because this example terminates during the encoding operation.

    Encoding ae = Encoding.GetEncoding(
                  "us-ascii",
                  new EncoderExceptionFallback(),
                  new DecoderExceptionFallback());

// The input string consists of the Unicode characters LEFT POINTING
// DOUBLE ANGLE QUOTATION MARK (U+00AB), 'X' (U+0058), and RIGHT POINTING
// DOUBLE ANGLE QUOTATION MARK (U+00BB).
// The encoding can only encode characters in the US-ASCII range of U+0000
// through U+007F. Consequently, the characters bracketing the 'X' character
// cause an exception.
    string inputString = "\u00abX\u00bb";

    string twoNewLines = "\n\n";
    byte[] encodedBytes = new byte[ae.GetMaxByteCount(inputString.Length)];
    int numberOfEncodedBytes = 0;

// --------------------------------------------------------------------------
    Console.Clear();

// Display the name of the encoding.
    Console.WriteLine("The name of the encoding is \"{0}\".\n", ae.WebName);

// Display the input string in text.
    Console.WriteLine("Input string ({0} characters): \"{1}\"",
                       inputString.Length, inputString);

// Display the input string in hexadecimal.
    Console.Write("Input string in hexadecimal: ");
    foreach (char c in inputString.ToCharArray()) {
        Console.Write("0x{0:X2} ", (int)c);}
    Console.Write(twoNewLines);

// --------------------------------------------------------------------------
// Attempt to encode the input string. However, an exception is thrown before
// the input string can be encoded.

    Console.WriteLine("Encode the input string...");

// The code example terminates during the call to the GetBytes() method.
    try {
        numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length,
                                           encodedBytes, 0);
        }
    catch (EncoderFallbackException e)
        {
        Console.WriteLine(e);
        Console.WriteLine("\n*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***");
        return;
        }

// This statement is never executed.
    Console.WriteLine("This statement is never executed.");
    }
}
/*
This code example produces the following results:

The name of the encoding is "us-ascii".

Input string (3 characters): "«X»"
Input string in hexadecimal: 0xAB 0x58 0xBB

Encode the input string...
System.Text.EncoderFallbackException: Unable to translate Unicode character \u00AB at inde
x 0 to specified code page.
   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
   at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
   at System.Text.ASCIIEncoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32
byteCount, EncoderNLS encoder)
   at System.Text.ASCIIEncoding.GetBytes(String chars, Int32 charIndex, Int32 charCount, B
yte[] bytes, Int32 byteIndex)
   at Sample.Main()

*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***

*/
' This example demonstrates the EncoderExceptionFallback class.
Imports System.Text

Class Sample
    Public Shared Sub Main() 
        
        ' Create an encoding, which is equivalent to calling the 
        ' ASCIIEncoding class constructor. 
        ' The EncoderExceptionFallback parameter causes an exception to
        ' be thrown when a character cannot be encoded. 
        ' A decoder exception fallback is also specified, but it is not 
        ' used because this example terminates during the encoding operation.  


        Dim eef As New EncoderExceptionFallback()
        Dim def As New DecoderExceptionFallback()
        Dim ae As Encoding = Encoding.GetEncoding("us-ascii", eef, def)
        
        ' The input string consists of the Unicode characters LEFT POINTING 
        ' DOUBLE ANGLE QUOTATION MARK (U+00AB), 'X' (U+0058), and RIGHT POINTING 
        ' DOUBLE ANGLE QUOTATION MARK (U+00BB). 
        ' The encoding can only encode characters in the US-ASCII range of U+0000 
        ' through U+007F. Consequently, the characters bracketing the 'X' character
        ' cause an exception.

        Dim inputString As String = "«X»"
        
        Dim twoNewLines As String = vbCrLf & vbCrLf
        Dim numberOfEncodedBytes As Integer = ae.GetMaxByteCount(inputString.Length)
        ' Counteract the compiler adding an extra element.
        Dim encodedBytes(numberOfEncodedBytes - 1) As Byte
        
        ' --------------------------------------------------------------------------
        Console.Clear()
        
        ' Display the name of the encoding.
        Console.WriteLine("The name of the encoding is ""{0}""." & vbCrLf, ae.WebName)
        
        ' Display the input string in text.
        Console.WriteLine("Input string ({0} characters): ""{1}""", _
                           inputString.Length, inputString)
        
        ' Display the input string in hexadecimal. 
        ' Each element is converted to an integer with Convert.ToInt32.
        Console.Write("Input string in hexadecimal: ")
        Dim c As Char
        For Each c In  inputString.ToCharArray()
            Console.Write("0x{0:X2} ", Convert.ToInt32(c))
        Next c
        Console.Write(twoNewLines)
        
        ' --------------------------------------------------------------------------
        ' Attempt to encode the input string. However, an exception is thrown before
        ' the input string can be encoded.
        Console.WriteLine("Encode the input string...")
        
        ' The code example terminates during the call to the GetBytes() method.
        Try
            numberOfEncodedBytes = ae.GetBytes(inputString, 0, inputString.Length, _
                                               encodedBytes, 0)
        Catch e As EncoderFallbackException
            Console.WriteLine(e)
            Console.WriteLine(vbCrLf & _
                              "*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***")
            Return
        End Try
        
        ' This statement is never executed.
        Console.WriteLine("This statement is never executed.")
    
    End Sub
End Class
'
'This code example produces the following results:
'
'The name of the encoding is "us-ascii".
'
'Input string (3 characters): "X"
'Input string in hexadecimal: 0xAB 0x58 0xBB
'
'Encode the input string...
'System.Text.EncoderFallbackException: Unable to translate Unicode character \u00AB at inde
'x 0 to specified code page.
'   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
'   at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
'   at System.Text.ASCIIEncoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32
'byteCount, EncoderNLS encoder)
'   at System.Text.ASCIIEncoding.GetBytes(String chars, Int32 charIndex, Int32 charCount, B
'yte[] bytes, Int32 byteIndex)
'   at Sample.Main()
'
'*** THE CODE EXAMPLE TERMINATES HERE AS INTENDED. ***
'

설명

인코딩은 유니코드 문자를 인코딩된 바이트 시퀀스에 매핑합니다. 이 시퀀스는 이후에 디스크와 같은 실제 매체 또는 통신 링크를 통해 전송될 수 있습니다. 문자는 다양한 방법으로 매핑할 수 있으며 특정 인코딩은 클래스에서 Encoding 파생된 형식으로 표시됩니다. 특히 인코딩 형식의 GetBytes 메서드는 문자를 바이트 시퀀스로 인코딩하고 GetChars 바이트 시퀀스를 문자로 디코딩합니다.

입력된 문자를 인코딩하여 표현할 수 없는 경우 인코딩 작업이 실패할 수 있습니다. 예를 들어 개체는 ASCIIEncoding U+0000에서 U+007F 범위 밖에 있는 유니코드 코드 포인트 값을 생성하는 문자를 인코딩할 수 없습니다.

인코딩 또는 디코딩 변환을 수행할 수 없는 경우 .NET Framework 대체(fallback)라는 오류 처리 메커니즘을 제공합니다. 애플리케이션 미리 정의 된.NET Framework 인코더 대체 (fallback)를 사용 하거나에서 파생 된 사용자 지정 인코더 대체 (fallback)를 만들 수는 EncoderFallbackEncoderFallbackBuffer 클래스입니다.

이 .NET Framework 인코딩 변환 오류를 처리하기 위한 서로 다른 대체 전략을 구현하는 두 가지 미리 정의된 클래스를 제공합니다. 클래스는 EncoderReplacementFallback 변환할 수 없는 입력 문자에 대해 제공된 문자열을 대체합니다. 대체 문자열은 잘못된 문자 대신 인코딩된 다음 인코딩 작업에서 입력의 나머지 부분을 계속 변환합니다. 반면, 클래스는 EncoderExceptionFallback EncoderFallbackException 잘못된 문자가 발견되면 throw합니다.

생성자

EncoderExceptionFallback()

EncoderExceptionFallback 클래스의 새 인스턴스를 초기화합니다.

속성

MaxCharCount

이 인스턴스가 반환할 수 있는 최대 문자 수를 가져옵니다.

메서드

CreateFallbackBuffer()

문자 시퀀스를 바이트 시퀀스로 변환할 수 없으면 인코더 대체(fallback) 버퍼는 예외를 throw합니다.

Equals(Object)

현재 EncoderExceptionFallback 개체와 지정된 개체가 같은지 여부를 나타냅니다.

GetHashCode()

이 인스턴스의 해시 코드를 검색합니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보