欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# System.TypeInitializationException 異常處理方案

 更新時(shí)間:2023年02月26日 14:42:53   作者:yangzm996  
這篇文章主要介紹了C# System.TypeInitializationException 異常處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C# System.TypeInitializationException 異常處理

備忘

問(wèn)題在這

這種錯(cuò)誤大多是聲明的類(lèi)里面初始字段賦值出了問(wèn)題

比如 在類(lèi)里面生命了一個(gè) 太大的數(shù)組,超出了最大內(nèi)存限制就會(huì)出錯(cuò)

修改下就OK了

C#基礎(chǔ)--錯(cuò)誤和異常

異常類(lèi)

在c#中,當(dāng)出現(xiàn)某個(gè)特殊的異常錯(cuò)誤條件時(shí),就會(huì)創(chuàng)建(或拋出)一個(gè)異常對(duì)象。這個(gè)對(duì)象包含有助于跟蹤問(wèn) 題的信息。我們可以創(chuàng)建自己的異常類(lèi),但.NET提供了許多預(yù)定義的異常類(lèi),多到這里不可能 提供詳盡的列表。

列舉幾個(gè)常見(jiàn)異常:

  • StackOverflowException—如果分配給棧的內(nèi)存區(qū)域己滿(mǎn),就會(huì)拋出這個(gè)異常。
  • EndOfStreamException—這個(gè)異常通常是因?yàn)樽x到文件末尾而拋出的。
  • OverflowException—如果要在checked上下文中把包含值-40的int類(lèi)型數(shù)據(jù)強(qiáng)制轉(zhuǎn)換為uint數(shù)據(jù),就會(huì)拋出這個(gè)異常。

捕獲異常

  • try塊包含的代碼組成了程序的正常操作部分,但這部分程序可能遇到某些嚴(yán)重的錯(cuò)誤。
  • catch塊包含的代碼處理各種錯(cuò)誤情況,這些錯(cuò)誤是執(zhí)行try塊中的代碼時(shí)遇到的。這個(gè)塊還可以用于記 錄錯(cuò)誤。
  • finally塊包含的代碼清理資源或執(zhí)行通常要在try塊或catch塊末尾執(zhí)行的其他操作。無(wú)論是否拋出異常,都會(huì)執(zhí)行finally塊,理解這一點(diǎn)非常重要。因?yàn)閒inally塊包含了應(yīng)總是執(zhí)行的清理代碼,如果 在finally塊中放置了return語(yǔ)句,編譯器就會(huì)標(biāo)記一個(gè)錯(cuò)誤。

下面的步驟說(shuō)明了這些塊是如何組合在一起捕獲錯(cuò)誤情況的:

(1) 執(zhí)行的程序流進(jìn)入try塊。

(2) 如果在try塊中沒(méi)有錯(cuò)誤發(fā)生,在塊中就會(huì)正常執(zhí)行操作。當(dāng)程序流到達(dá)try塊末尾后,如果存在一個(gè)finally塊,程序流就會(huì)自動(dòng)SA finally塊(第(5)步)。但如果在try塊中程序流檢測(cè)到一個(gè)錯(cuò)誤,程序流就會(huì)跳轉(zhuǎn) 到catch塊(第⑶步)。

(3) 在catch塊中處理錯(cuò)誤。

(4) 在catch塊執(zhí)行完后,如果存在一個(gè)finally塊,程序流就會(huì)自動(dòng)進(jìn)入finally塊:

(5) 執(zhí)行finally塊(如果存在)。

try
{
    
}
catch (Exception ex)
{
    
}
finally
{
    
}

異常性能

異常處理具有性能含義。在常見(jiàn)的情況下,不應(yīng)該使用異常處理錯(cuò)誤。例如,將字符串轉(zhuǎn)換為數(shù)字時(shí),可 以使用int類(lèi)型的Paree方法。如果傳遞給此方法的字符串不能轉(zhuǎn)換為數(shù)字,此方法拋FormatException異常;如果可以轉(zhuǎn)換一個(gè)數(shù)字,但它不能放在int類(lèi)型中,則拋出OverflowException異常:

static void NumberDemol(string n)
{
?? ?if (n is null) throw new ArgumentNullException(nameof(n));?
?? ?try
?? ?{
?? ??? ?int i = int.Parse(n);
?? ??? ?Console.WriteLine($"converted: {i}");
?? ?}
?? ?catch (FormatException ex)
?? ?{
?? ??? ?Console.WriteLine(ex.Message);
?? ?}
?? ?catch (OverflowException ex)
?? ?{
?? ??? ?Console.WriteLine(ex.Message);
?? ?}
}

如果NumberDemol方法通常只用于在字符串中傳遞數(shù)字而接收不到數(shù)字是異常的,那么可以這樣編寫(xiě)它。 但是,如果在程序流的正常情況下,期望的字符串不能轉(zhuǎn)換時(shí),可以使用TryParse方法。如果字符串不能轉(zhuǎn)換 為數(shù)字,此方法不會(huì)拋出異常。相反,如果解析成功,TryParse返回true;如果解析失敗,則返回felse:

static void NumberDemo2(string n)
{
?? ?if (n is null) throw new ArgumentNullException(nameof(n));?
?? ?if (int.TryParse(n, ? out int result))
?? ?{
?? ??? ?Console. WriteLine ($"converted {result}");
?? ?}
?? ?else
?? ?{
?? ??? ?Console.WriteLine("not a number");
?? ?}
}

實(shí)現(xiàn)多個(gè)catch塊

class Program
{
? ? static void Main()
? ? {
? ? ? ? while (true)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string userInput;

? ? ? ? ? ? ? ? Console.Write("Input a number between 0 and 5 or just hit return to exit)> ");
? ? ? ? ? ? ? ? userInput = Console.ReadLine();

? ? ? ? ? ? ? ? if (string.IsNullOrEmpty(userInput))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? int index = Convert.ToInt32(userInput);

? ? ? ? ? ? ? ? if (index < 0 || index > 5)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? throw new IndexOutOfRangeException($"You typed in {userInput}");
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Console.WriteLine($"Your number was {index}");
? ? ? ? ? ? }
? ? ? ? ? ? catch (IndexOutOfRangeException ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"Exception: Number should be between 0 and 5. {ex.Message}");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine($"An exception was thrown. Message was: {ex.Message}");
? ? ? ? ? ? }
? ? ? ? ? ? finally
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("Thank you\n");
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

異常過(guò)濾器

自從C# 6開(kāi)始就支持異常過(guò)濾器。catck塊僅在過(guò)濾器返回true時(shí)執(zhí)行。捕獲不同的異常類(lèi)型時(shí),可以有行為不同的catch塊。在某些情況下,catch塊基于異常的內(nèi)容執(zhí)行不同的操作。

class Program
{
? ? static void Main()
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? ThrowWithErrorCode(405);

? ? ? ? }
? ? ? ? catch (MyCustomException ex) when (ex.ErrorCode == 405)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"Exception caught with filter {ex.Message} and {ex.ErrorCode}");
? ? ? ? }
? ? ? ? catch (MyCustomException ex)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"Exception caught {ex.Message} and {ex.ErrorCode}");
? ? ? ? }

? ? ? ? Console.ReadLine();
? ? }

? ? public static void ThrowWithErrorCode(int code)
? ? {
? ? ? ? throw new MyCustomException("Error in Foo") { ErrorCode = code };
? ? }
}

自定義異常

這個(gè)示例稱(chēng)為SolicitColdCall,它包 含兩個(gè)嵌套的try塊,說(shuō)明了如何定義自定義異常類(lèi),再?gòu)膖ry塊中拋出另一個(gè)異常。

public class ColdCallFileFormatException : Exception
{
    public ColdCallFileFormatException(string message)
        : base(message)
    {
    }

    public ColdCallFileFormatException(string message, Exception innerException)
        : base(message, innerException)
    {
    }
}

public class SalesSpyFoundException : Exception
{
    public SalesSpyFoundException(string spyName)
      : base($"Sales spy found, with name {spyName}")
    {
    }

    public SalesSpyFoundException(string spyName, Exception innerException)
      : base($"Sales spy found with name {spyName}", innerException)
    {
    }
}

public class UnexpectedException : Exception
{
    public UnexpectedException(string message)
        : base(message)
    {
    }

    public UnexpectedException(string message, Exception innerException)
        : base(message, innerException)
    {
    }
}

public class ColdCallFileReader : IDisposable
{
    private FileStream _fs;
    private StreamReader _sr;
    private uint _nPeopleToRing;
    private bool _isDisposed = false;
    private bool _isOpen = false;

    public void Open(string fileName)
    {
        if (_isDisposed)
        {
            throw new ObjectDisposedException("peopleToRing");
        }

        _fs = new FileStream(fileName, FileMode.Open);
        _sr = new StreamReader(_fs);

        try
        {
            string firstLine = _sr.ReadLine();
            _nPeopleToRing = uint.Parse(firstLine);
            _isOpen = true;
        }
        catch (FormatException ex)
        {
            throw new ColdCallFileFormatException(
                $"First line isn\'t an integer {ex}");
        }
    }

    public void ProcessNextPerson()
    {
        if (_isDisposed)
        {
            throw new ObjectDisposedException("peopleToRing");
        }

        if (!_isOpen)
        {
            throw new UnexpectedException(
                "Attempted to access coldcall file that is not open");
        }

        try
        {
            string name = _sr.ReadLine();
            if (name == null)
            {
                throw new ColdCallFileFormatException("Not enough names");
            }
            if (name[0] == 'B')
            {
                throw new SalesSpyFoundException(name);
            }
            Console.WriteLine(name);
        }
        catch (SalesSpyFoundException ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
        }
    }

    public uint NPeopleToRing
    {
        get
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("peopleToRing");
            }

            if (!_isOpen)
            {
                throw new UnexpectedException(
                    "Attempted to access cold–call file that is not open");
            }

            return _nPeopleToRing;
        }
    }

    public void Dispose()
    {
        if (_isDisposed)
        {
            return;
        }

        _isDisposed = true;
        _isOpen = false;

        _fs?.Dispose();
        _fs = null;
    }
}

class Program
{
    static void Main()
    {
        Console.Write("Please type in the name of the file " +
            "containing the names of the people to be cold called > ");
        string fileName = Console.ReadLine();
        ColdCallFileReaderLoop1(fileName);
        Console.WriteLine();
        ColdCallFileReaderLoop2(fileName);
        Console.WriteLine();

        Console.ReadLine();
    }

    private static void ColdCallFileReaderLoop2(string fileName)
    {
        using (var peopleToRing = new ColdCallFileReader())
        {

            try
            {
                peopleToRing.Open(fileName);
                for (int i = 0; i < peopleToRing.NPeopleToRing; i++)
                {
                    peopleToRing.ProcessNextPerson();
                }
                Console.WriteLine("All callers processed correctly");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"The file {fileName} does not exist");
            }
            catch (ColdCallFileFormatException ex)
            {
                Console.WriteLine($"The file {fileName} appears to have been corrupted");
                Console.WriteLine($"Details of problem are: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"Inner exception was: {ex.InnerException.Message}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception occurred:\n{ex.Message}");
            }
        }
    }

    public static void ColdCallFileReaderLoop1(string fileName)
    {
        var peopleToRing = new ColdCallFileReader();

        try
        {
            peopleToRing.Open(fileName);
            for (int i = 0; i < peopleToRing.NPeopleToRing; i++)
            {
                peopleToRing.ProcessNextPerson();
            }
            Console.WriteLine("All callers processed correctly");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"The file {fileName} does not exist");
        }
        catch (ColdCallFileFormatException ex)
        {
            Console.WriteLine($"The file {fileName} appears to have been corrupted");
            Console.WriteLine($"Details of problem are: {ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"Inner exception was: {ex.InnerException.Message}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception occurred:\n{ex.Message}");
        }
        finally
        {
            peopleToRing.Dispose();
        }
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論