温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# System.TypeInitializationException异常如何处理

发布时间:2023-02-27 09:18:40 来源:亿速云 阅读:103 作者:iii 栏目:开发技术

这篇文章主要介绍“C# System.TypeInitializationException异常如何处理”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C# System.TypeInitializationException异常如何处理”文章能帮助大家解决问题。

C# System.TypeInitializationException 异常处理

备忘

C# System.TypeInitializationException异常如何处理

问题在这

C# System.TypeInitializationException异常如何处理

这种错误大多是声明的类里面初始字段赋值出了问题

比如 在类里面生命了一个 太大的数组,超出了最大内存限制就会出错

C# System.TypeInitializationException异常如何处理

修改下就OK了

C#基础--错误和异常

异常类

在c#中,当出现某个特殊的异常错误条件时,就会创建(或抛出)一个异常对象。这个对象包含有助于跟踪问 题的信息。我们可以创建自己的异常类,但.NET提供了许多预定义的异常类,多到这里不可能 提供详尽的列表。

列举几个常见异常:

  • StackOverflowException—如果分配给栈的内存区域己满,就会抛出这个异常。

  • EndOfStreamException—这个异常通常是因为读到文件末尾而抛出的。

  • OverflowException—如果要在checked上下文中把包含值-40的int类型数据强制转换为uint数据,就会抛出这个异常。

捕获异常

  • try块包含的代码组成了程序的正常操作部分,但这部分程序可能遇到某些严重的错误。

  • catch块包含的代码处理各种错误情况,这些错误是执行try块中的代码时遇到的。这个块还可以用于记 录错误。

  • finally块包含的代码清理资源或执行通常要在try块或catch块末尾执行的其他操作。无论是否抛出异常,都会执行finally块,理解这一点非常重要。因为finally块包含了应总是执行的清理代码,如果 在finally块中放置了return语句,编译器就会标记一个错误。

下面的步骤说明了这些块是如何组合在一起捕获错误情况的:

(1) 执行的程序流进入try块。

(2) 如果在try块中没有错误发生,在块中就会正常执行操作。当程序流到达try块末尾后,如果存在一个finally块,程序流就会自动SA finally块(第(5)步)。但如果在try块中程序流检测到一个错误,程序流就会跳转 到catch块(第⑶步)。

(3) 在catch块中处理错误。

(4) 在catch块执行完后,如果存在一个finally块,程序流就会自动进入finally块:

(5) 执行finally块(如果存在)。

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

异常性能

异常处理具有性能含义。在常见的情况下,不应该使用异常处理错误。例如,将字符串转换为数字时,可 以使用int类型的Paree方法。如果传递给此方法的字符串不能转换为数字,此方法抛FormatException异常;如果可以转换一个数字,但它不能放在int类型中,则抛出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方法通常只用于在字符串中传递数字而接收不到数字是异常的,那么可以这样编写它。 但是,如果在程序流的正常情况下,期望的字符串不能转换时,可以使用TryParse方法。如果字符串不能转换 为数字,此方法不会抛出异常。相反,如果解析成功,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");
    }
}

实现多个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");
            }
        }
    }
}

异常过滤器

自从C# 6开始就支持异常过滤器。catck块仅在过滤器返回true时执行。捕获不同的异常类型时,可以有行为不同的catch块。在某些情况下,catch块基于异常的内容执行不同的操作。

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 };
    }
}

自定义异常

这个示例称为SolicitColdCall,它包 含两个嵌套的try块,说明了如何定义自定义异常类,再从try块中抛出另一个异常。

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();
        }
    }
}

关于“C# System.TypeInitializationException异常如何处理”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI