Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
了解Java中检查的与未检查的异常_Java_Exception_Runtimeexception_Checked Exceptions_Unchecked Exception - Fatal编程技术网

了解Java中检查的与未检查的异常

了解Java中检查的与未检查的异常,java,exception,runtimeexception,checked-exceptions,unchecked-exception,Java,Exception,Runtimeexception,Checked Exceptions,Unchecked Exception,约书亚·布洛赫在《有效的爪哇》中说 将选中的异常用于 可恢复条件和运行时间 编程错误的例外情况 (第二版第58项) 让我们看看我是否理解正确 以下是我对选中异常的理解: try{ String userInput = //read in user input Long id = Long.parseLong(userInput); }catch(NumberFormatException e){ id = 0; //recover the situation by set

约书亚·布洛赫在《有效的爪哇》中说

将选中的异常用于 可恢复条件和运行时间 编程错误的例外情况 (第二版第58项)

让我们看看我是否理解正确

以下是我对选中异常的理解:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}
try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}
1。上述情况是否被视为已检查的例外情况?

public void someMethod throws Exception{

}
2。RuntimeException是否为未检查的异常?

public void someMethod throws Exception{

}
以下是我对未检查异常的理解:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}
try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}
4。现在,上面的代码不能也是一个已检查的异常吗?我可以试着恢复这种状况吗?我可以吗?(注意:我的第三个问题在上面的
catch
中)

5。人们为什么这样做?

public void someMethod throws Exception{

}
他们为什么让例外冒出来?更快地处理错误不是更好吗?为什么要冒泡

6。我应该冒泡出确切的异常还是使用异常屏蔽它?

public void someMethod throws Exception{

}
以下是我的阅读资料


许多人说,根本不应该使用已检查的异常(即您应该显式捕获或重新筛选的异常)。例如,它们在C#中被淘汰,而大多数语言都没有。因此,您可以始终抛出
RuntimeException
的子类(未选中的异常)


然而,我认为检查异常是有用的——当您想要强制API的用户考虑如何处理异常情况(如果它是可恢复的)时,可以使用检查异常。只是检查过的异常在Java平台中被过度使用,这让人们讨厌它们

关于具体问题:

< > >强> >是<代码> NoMultFraseExtabor >代码>考虑检查异常吗?< /强> BR> 否。
NumberFormatException
未选中(=是
RuntimeException
的子类)。为什么?我不知道。(但是应该有一个方法
isValidInteger(..)

  • 运行时异常是否为未检查的异常?
    是的,没错

  • 我应该在这里做什么?
    这取决于代码所在的位置以及您希望发生什么。如果它在UI层-捕获它并显示警告;如果它在服务层-根本不要抓住它-让它冒泡。只是不要接受例外。如果在大多数情况下发生异常,您应选择以下其中一种:

    • 记录并返回
    • 重新抛出它(声明该方法将抛出它)
    • 通过在构造函数中传递当前异常来构造新异常
  • 现在,上面的代码不能也是一个已检查的异常吗?我可以试着恢复这种状况吗?我可以吗?
    可能是。但是没有什么可以阻止您捕获未检查的异常

  • 为什么人们要在throws子句中添加class
    Exception

    最常见的原因是人们懒惰地考虑要抓什么,什么要扔。抛出异常是一种不好的做法,应该避免


  • 遗憾的是,没有一条规则可以让您确定何时捕获、何时重试、何时使用选中异常以及何时使用未选中异常。我同意这会导致很多混乱和很多糟糕的代码。一般原则由Bloch陈述(您引用了其中的一部分)。一般原则是将异常重定向到您可以处理它的层。

    许多人说,根本不应该使用已检查的异常(即您应该显式捕获或重定向的异常)。例如,它们在C#中被淘汰,而大多数语言都没有。因此,您可以始终抛出
    RuntimeException
    的子类(未选中的异常)


    然而,我认为检查异常是有用的——当您想要强制API的用户考虑如何处理异常情况(如果它是可恢复的)时,可以使用检查异常。只是检查过的异常在Java平台中被过度使用,这让人们讨厌它们

    关于具体问题:

    < > >强> >是<代码> NoMultFraseExtabor >代码>考虑检查异常吗?< /强> BR> 否。
    NumberFormatException
    未选中(=是
    RuntimeException
    的子类)。为什么?我不知道。(但是应该有一个方法
    isValidInteger(..)

  • 运行时异常是否为未检查的异常?
    是的,没错

  • 我应该在这里做什么?
    这取决于代码所在的位置以及您希望发生什么。如果它在UI层-捕获它并显示警告;如果它在服务层-根本不要抓住它-让它冒泡。只是不要接受例外。如果在大多数情况下发生异常,您应选择以下其中一种:

    • 记录并返回
    • 重新抛出它(声明该方法将抛出它)
    • 通过在构造函数中传递当前异常来构造新异常
  • 现在,上面的代码不能也是一个已检查的异常吗?我可以试着恢复这种状况吗?我可以吗?
    可能是。但是没有什么可以阻止您捕获未检查的异常

  • 为什么人们要在throws子句中添加class
    Exception

    最常见的原因是人们懒惰地考虑要抓什么,什么要扔。抛出异常是一种不好的做法,应该避免

  • 遗憾的是,没有一条规则可以让您确定何时捕获、何时重试、何时使用选中异常以及何时使用未选中异常。我同意这会导致很多混乱和很多糟糕的代码。一般原则由Bloch陈述(您引用了其中的一部分)。一般原则是将异常重新提交到可以处理它的层

  • 是否将上述情况视为已检查的例外情况? 不 事实上
    try {
        String userAge = (String)request.getParameter("age");
        userObject.setAge(Integer.parseInt(strUserAge));
    } catch (NumberFormatException npe) {
       sendError("Sorry, Age is supposed to be an Integer. Please try again.");
    }
    
     java.lang.Object
     extended by java.lang.Throwable
      extended by java.lang.Exception
       extended by java.lang.RuntimeException  //<-NumberFormatException is a RuntimeException  
        extended by java.lang.IllegalArgumentException
         extended by java.lang.NumberFormatException
    
    public void myMethod() throws Exception {
        // ... something that throws FileNotFoundException ...
    }
    
    String s = "abc";
    Object o = s;
    Integer i = (Integer) o;
    
    Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
        at Sample.main(Sample.java:9)
    
    public interface IFoo {
        public void foo();
    }
    
    public class Foo implements IFoo {
        @Override
        public void foo() {
            System.out.println("I don't throw and exception");
        }
    }
    
    public class Bar implements IFoo {
        @Override
        public void foo() {
            //I'm using InterruptedExcepton because you probably heard about it somewhere. It's a checked exception. Any checked exception will work the same.
            throw new InterruptedException();
        }
    }
    
    public class Bar implements IFoo {
        @Override
        public void foo() throws InterruptedException {
            throw new InterruptedException();
        }
    }