Skip to content

面试速答(先看这里)

**一句话结论:**异常的处理方式有两种。

60秒标准回答:

javac使用了语法糖进行优化

异常的处理方式有两种。1、自己处理。2、向上抛,交给调用者处理

异常,千万不能捕获了之后什么也不做。或者只是使用e.printStacktrace

**答题顺序:**结论 → 原理/机制 → 关键流程 → 场景与取舍 → 易错点

回答主线:

  • **try-with-resource的原理:**javac使用了语法糖进行优化
  • **Java7中还对异常做了哪些优化?:**Multi-Catch Exceptions,可以连续处理多个异常,如:
  • **Java中异常的处理方式有哪几种?一般如何选择。:**异常的处理方式有两种。

**记忆锚点:**Exceptions → catch → throws → try-with-resource的原理 → try-with-resource → eprintStacktrace

易错提醒:

  • 异常,千万不能捕获了之后什么也不做。

加分表达:

  • #start 方法不会发生IOException,所以不需要throws RuntimeExcption不需要显式的throws catch的时候,要先从子类开始catch,代码中catch的顺序不对 return之前的finally block是会被执行的 上述代码,如何优化 try-with-resource的原理 javac使用了语法糖进行优化 Jav…

追问准备:

  • 围绕「Exceptions」:底层原理是什么?使用时有哪些边界和常见坑?
  • 围绕「catch」:底层原理是什么?使用时有哪些边界和常见坑?
  • 围绕「throws」:底层原理是什么?使用时有哪些边界和常见坑?
  • 如果线上出现异常,你会如何定位、验证并规避?

java
public static void start() throws IOException, RuntimeException{
    throw new RuntimeException("Not able to Start");
 }

 public static void main(String args[]) {
    BufferedReader br = null;
    try {
        String line;
        br = new BufferedReader(new FileReader("\home\usr\test.java"));
        while ((line = br.readLine()) != null) {
            start();
        }
        return;
    } catch (Exception ex) {
        ex.printStackTrace();
    } catch (RuntimeException re) {
        re.printStackTrace();
    } finally {
        // 是否会输出?
        System.out.print("1");
    }
 }

典型回答 ​

  1. #start方法不会发生IOException,所以不需要throws
  2. RuntimeExcption不需要显式的throws
  3. catch的时候,要先从子类开始catch,代码中catch的顺序不对
  4. 没有关闭流
  5. return之前的finally block是会被执行的

知识扩展 ​

上述代码,如何优化 ​

java
public static void main(String... args) {
    try (BufferedReader br = new BufferedReader(new FileReader("\home\usr\test.java"))) {
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        // handle exception
    }
}

try-with-resource的原理 ​

javac使用了语法糖进行优化

java
public static void main(String args[]) {
    BufferedReader br;
    Throwable throwable;
    br = new BufferedReader(new FileReader("\home\usr\test.java"));
    throwable = null;
    String line;
    try {
        while((line = br.readLine()) != null)
        System.out.println(line);
    } catch(Throwable throwable2) {
        throwable = throwable2;
        throw throwable2;
    }finally{
        if(br != null)
        if(throwable != null)
        try {
        	br.close();
        } catch(Throwable throwable1) {
        	throwable.addSuppressed(throwable1);
        }
    else
    	br.close();
    }
}

Java7中还对异常做了哪些优化? ​

  1. Multi-Catch Exceptions,可以连续处理多个异常,如:
java
public class ExampleExceptionHandlingNew
{
   public static void main( String[] args )
   {
    try {
        URL url = new URL("http://www.yoursimpledate.server/");
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(url.openStream()));
        String line = reader.readLine();
        SimpleDateFormat format = new SimpleDateFormat("MM/DD/YY");
        Date date = format.parse(line);
    }
    catch(ParseException | IOException exception) {
        // handle our problems here.
    }
   }
}
  1. Rethrowing Exceptions
  2. Suppressed Exceptions
  3. 参考网址

Java中异常的处理方式有哪几种?一般如何选择。 ​

异常的处理方式有两种。1、自己处理。2、向上抛,交给调用者处理。

异常,千万不能捕获了之后什么也不做。或者只是使用e.printStacktrace。

具体的处理方式的选择其实原则比较简明:自己明确的知道如何处理的,就要处理掉。不知道如何处理的,就交给调用者处理