zoukankan      html  css  js  c++  java
  • 异常Exception(三)

    上一篇:异常Exception(二)

    使用try...catch...捕获异常,如果能预料到某些异常可能发生,可以使用精确的异常例如“FileNotFoundException”、“DirectoryNotFoundException”、“IOException”等,最有使用一般异常“Exception”。

    using System;
    using System.Collections;
    using System.IO;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    using (StreamReader sr = File.OpenText("data.txt"))
                    {
                        Console.WriteLine($"The first line of this file is {sr.ReadLine()}");
                    }
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine($"The file was not found: '{e}'");
                }
                catch (DirectoryNotFoundException e)
                {
                    Console.WriteLine($"The directory was not found: '{e}'");
                }
                catch (IOException e)
                {
                    Console.WriteLine($"The file could not be opened: '{e}'");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"其他异常:{e}");
                }
                Console.ReadLine();
            }    
        }
    }
    View Code

    使用throw,且throw添加异常对象,显示抛出异常。throw后面什么都不加,抛出当前异常。

    using System;
    using System.IO;
    
    namespace ConsoleApp5
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileStream fs=null;
                try
                {
                    fs = new FileStream(@"C:	empdata.txt", FileMode.Open);
                    var sr = new StreamReader(fs);
    
                    string line = sr.ReadLine();
                    Console.WriteLine(line);
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine($"[Data File Missing] {e}");
                    throw new FileNotFoundException(@"[data.txt not in c:	emp directory]", e);
                    // 直接使用throw相当抛出当前异常。
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                        Console.WriteLine("关掉");
                    }
                }
                Console.ReadLine();
            }    
        }
    }
    View Code

    自定义异常:继承System.Exception。自定义的异常命名应该以“Exception”结尾,例如“StudentNotFoundException”。在使用远程调用服务时,服务端的自定义异常要保证客户端也可用,客户端包括调用方和代理。

    有时候发生的异常导致后面的代码无法处理,如果后面的代码涉及到清理资源应当小心,这时应该在finally模块放入清理资源的代码。

  • 相关阅读:
    看懂Oracle执行计划
    pl/sql编译存储过程卡住的解决方法
    ORA-00604的解决方法
    ORACLE SYNONYM详解
    Oracle 用户权限管理方法
    “ORA-06550: 第 1 行, 第 7 列”解决方法
    oracle 存储过程给另一个用户的权限问题
    Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile
    eclipse maven maven-compiler-plugin 报错 完全解决
    如何用git把本地代码上传到github
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/exception3.html
Copyright © 2011-2022 走看看