美文网首页
10.5 关于Java异常的十大问题

10.5 关于Java异常的十大问题

作者: 明翼 | 来源:发表于2018-05-03 22:47 被阅读19次

    本文总结了关于Java异常的前10个常见问题。

    1、检查与未检查异常

    简而言之,检查的异常必须明确地在方法中捕获或在方法的throws子句中声明。 未检查的异常是由无法解决的问题(例如除以零,空指针等)引起的。检查异常尤其重要,因为您期望使用您的API的其他开发人员知道如何处理异常。

    例如,IOException是一个常用的检查异常,RuntimeException是一个未经检查的异常。 在阅读其余部分之前,您可以查看Java异常层次结构图。

    2.异常管理的最佳做法

    如果可以正确处理异常,则应该被捕获,否则应该抛出异常。

    3.为什么try中定义的变量不能用于catch或finally?

    在下面的代码中,在try子句中声明的字符串不能用在catch子句中。 该代码不通过编译。

    try {
        File file = new File("path");
        FileInputStream fis = new FileInputStream(file);
        String s = "inside";
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.out.println(s);
    }
    

    原因是你不知道在try块中哪里会抛出异常。 在声明对象之前抛出异常是很有可能的。 对于这个特定的例子,这是真的。

    4.为什么Double.parseDouble(null)和Integer.parseInt(null)会抛出不同的异常?

    他们实际上抛出不同的例外 这是JDK的问题。 它们由不同的开发人员开发,所以不值得太多思考。

    Integer.parseInt(null); 
    // throws java.lang.NumberFormatException: null
     
    Double.parseDouble(null); 
    // throws java.lang.NullPointerException
    

    5.在Java中常用的运行时异常

    这只是其中的一部分。
    抛出:IllegalArgumentException
    ArrayIndexOutOfBoundsException异常

    当条件不满足时,它们可用于if语句中,如下所示:

    if (obj == null) {
    throw new IllegalArgumentException("obj can not be null");
    }
    

    6.我们可以在同一个catch子句中捕获多个异常吗?

    答案是肯定的。 只要这些异常类可以追溯到类继承层次结构中的同一个超类,就可以只使用该超类。

    7.构造函数可以在java中引发异常吗?

    答案是肯定的。 构造函数是一种特殊的方法。 这是一个代码示例。

    class FileReader{
        public FileInputStream fis = null;
     
        public FileReader() throws IOException{
            File dir = new File(".");//get current directory
            File fin = new File(dir.getCanonicalPath() + File.separator + "not-existing-file.txt");
            fis = new FileInputStream(fin);
        }
    }
    

    8.在finlly中抛出异常

    执行以下操作是合法的:

    public static void main(String[] args) {
        File file1 = new File("path1");
        File file2 = new File("path2");
        try {
     
            FileInputStream fis = new FileInputStream(file1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                FileInputStream fis = new FileInputStream(file2);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    

    但为了获得更好的代码可读性,您应该将嵌入式try-catch块作为新方法进行包装,然后将方法调用放在finally子句中。

    public static void main(String[] args) {
        File file1 = new File("path1");
        File file2 = new File("path2");
        try {
     
            FileInputStream fis = new FileInputStream(file1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            methodThrowException();
        }
    }
    

    9. Can return be used in finally block

    是的。

    10.为什么开发人员默默地使用异常?

    有如此多的时间代码段会发生如下情况。 如果正确处理异常非常重要,为什么开发人员仍然这样做?

    try {
         ...
    } catch(Exception e) {
         e.printStackTrace();
    }
    

    忽略是很容易的。 频繁出现并不意味着正确。

    相关文章

      网友评论

          本文标题:10.5 关于Java异常的十大问题

          本文链接:https://www.haomeiwen.com/subject/wnqrrftx.html