例外简介-章 3

第3章:最常见的异常是什么?它们是什么意思?

正如我们在上一章中看到的那样,不仅有标准的Exception,而且还有一些特殊的异常,例如

NullPointerExceptionArrayIndexOutOfBoundsException 。 所有这些扩展了基本类Exception

通常,您可以将异常分为两类:已检查和未检查的异常。 Checked Exception由编译器在编译时检查。 您将用来控制程序流程的大多数异常将被选中。

未经检查的异常会在运行时引发,通常意味着您在某个地方犯了一个错误,但编译器不知道这一点。

所有未检查的异常都会扩展

RuntimeException ,而所有选中的Exception均不。

有一些例外,您会经常遇到。 我已尝试列出最常见的清单,并解释您可以找到的内容,来源。 您应该始终查看从编译器或运行时获得的消息-它会为您提供遇到错误的那一行。

  • ArrayIndexOutOfBoundsException

    就像他们大多数人都应该做的那样,这个异常描述了哪里出了错-某些Array索引超出了给定范围。 因此,例如,您有一个像这样的数组
    int [] array = new int[3]
    并且您正在尝试访问不存在的元素,也许像这样
    array[3]
    这是不正确的,因为数组以元素0开头。

    使用经常运行的for循环时,经常会出现此错误。 检查给定的行,找出为什么它试图找到一个不存在的元素。

    ArrayIndexOutOfBoundsException是未经检查的异常。
  • ClassCastException

    如果遇到ClassCastException,则某个地方存在无效的强制转换。 例如,以下代码引发ClassCastException:

    
    public class Exceptions {
        public static void main(String[] args) {
            // Anything extends java.lang.Object directly or indirectly and is therefore an Object
            Object testClass1 = new Class1("1");
            Object testClass2 = (Class2) testClass1;
        }
    } 
    class Class1
    {
        String x; 
        public Class1(String x)
        {
            this.x = x;
        }
    } 
    class Class2
    {
        int x; 
        public Class2(int x)
        {
            this.x = x;
        }
    } 
    这样做是因为它无法将Class1对象转换为Class2对象。 如果您自己创建了类,则通常必须告诉编译器如何将它们强制转换为另一个类:

    
    public class Exceptions {
        public static void main(String[] args) {
            // Now we've defined the cast ourselves and don't need to define the new Objects as Objects.
            Class1 testClass1 = new Class1("1");
            Class2 testClass2 = testClass1.toClass2(); 
            // If we do want to define them as Objects, it would work like this:
            Object testClass1_2 = new Class1("1");
            Object testClass2_2 = ((Class1) testClass1_2).toClass2();
        }
    } 
    class Class1
    {
        String x; 
        public Class1(String x)
        {
            this.x = x;
        } 
        public Class2 toClass2()
        {
            return new Class2(Integer.parseInt(x));
        }
    } 
    class Class2
    {
        int x; 
        public Class2(int x)
        {
            this.x = x;
        }
    } 
    ClassCastException是未经检查的异常。
  • ClassNotFoundException

    这意味着找不到某个类。 通常,当您编译类并尝试运行它们而不编译其他需要的类时,通常会发生这种情况。 这也可能意味着所需的类已编译,但不是最新的。 尝试重新编译所需的一切。
  • FileNotFoundException

    当您尝试访问文件时,该文件可能不存在。 此代码将引发FileNotFoundException:

    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException; 
    public class Exceptions {
        public static void main(String[] args) throws FileNotFoundException {
            File file = new File("hello.txt");
            FileInputStream iStream = new FileInputStream(file);
        }
    } 
    警告:请没有主方法,该方法会引发异常。 仅应出于演示目的进行此操作。 捕获并处理异常。 处理这种情况的更好方法是:

    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException; 
    public class Exceptions {
        public static void main(String[] args){
            try
            {
                File file = new File("hello.txt");
                FileInputStream iStream = new FileInputStream(file);
            }
            catch(FileNotFoundException fnfe)
            {
                System.err.println(fnfe);
            }
        }
    } 
    ClassNotFoundException是已检查的异常。
  • IOException: Input-Output-Exception是一个异常,这是非常常用的。 FileNotFoundException就是这些之一,因此可以被捕获:

    catch(IOException ioe) {
       //...
    } 
    以下代码将引发IOException:

    public static void throwIOException() throws IOException
    {
       try 
       {
           OutputStream oStream = new FileOutputStream(new File("")); 
          oStream.close(); 
          oStream.write(0); 
       }
       // The FileNotFoundException extends IOException - however to get a basic 
       // IOException, we do this: 
       catch(IOException fnfe) 
       {
          IOException ioe = new IOException(); 
          ioe.initCause(fnfe);
          throw ioe; 
        }
    } 
    这也是两件事的示例:
    • 包装的异常-try-catch-block捕获一个异常并引发另一个异常。 在某些情况下这可能非常有用。
    • 创建链接的异常。 这意味着,异常携带有关其来源的信息(即“ FileNotFoundException”)。 此信息的添加是通过ioe.initCause(fnfe);行完成的。 可以保留

      
      catch(IOException ioe) {
      {
         System.err.println(ioe); 
         System.err.println("Cause: " + ioe.getCause()); 
      } 
    IOException是一个检查的异常。
  • 空指针异常
    定义对象后,将抛出NullPointerException,但在尝试访问它时未声明。 要再次使用上一个示例,此代码将引发NullPointerException:
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException; 
    public class Exceptions {
        public static File file;
        public static void main(String[] args){
            try
            {
                FileInputStream iStream = new FileInputStream(file);
            }
            catch(FileNotFoundException fnfe)
            {
                System.err.println(fnfe);
            }
        }
    } 
    检查是否已声明要在异常给定的行中使用的Object。
    NullPointerException是未经检查的异常。
您当然可能会遇到此列表中未提及的异常。 通常,只需检查异常所提到的行并考虑异常的名称,便可以找到解决方案。 返回第2章继续第4章
附加的文件
文件类型:zip Chapter3.zip (1.5 KB,120视图)

From: https://bytes.com/topic/java/insights/750362-introduction-exceptions-ch-3-a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值