The most common Java interview questions and answers Summary (six)

abnormal

74. The difference between the throw and the throws?

throws exception information is used to declare all a method may throw, throws a statement exception but does not handle, but the anomaly to upload, who would call me to whom treatment. And throw it refers to a specific type of exception thrown.

 

75. final, finally, finalize what is the difference?

  • final can be modified classes, variables, methods, modifying class represents the class can not be inherited, modified method indicates that the method can not be overridden, modified variable indicates that the variable is a constant that can not be reassigned.

  • finally a general role in the try-catch block of code, in the handling of an exception, the finally block of code method Usually we will have to perform in shows regardless of whether an exception occurs, the code block will be executed, usually used to store some of the closed Resources code.

  • finalize is a method, a method of the Object class belong, and Object class is the class of the parent class, this method is generally invoked by the garbage collector, when we call the System's gc () method, by the garbage collector calls finalize (),recycle rubbish. 

 

76. try-catch-finally in which part can be omitted?

A: catch can be omitted

the reason:

More stringent argument is: try only suitable for handling runtime exceptions, try + catch + for abnormal general exception handling operation. That is, if you just try to deal with common abnormal not to be catch processing, compiling a pass, because the mandatory compiler, common abnormal if you choose to capture, display must be declared with catch for further processing. The run-time exception is not so specified at compile time, so the catch can be omitted, plus catch you feel compiler also understandable.

In theory, any code compiler is not pleasing to the eye to see, I feel there may be a potential problem, so even if you try to add all the code, the code at runtime when it is only plus a layer of skin on the basis of normal operation. But once you add a piece of code to try, it is tantamount to show commitment to the compiler, rather than to capture anomalies in the code that might be thrown thrown up processing. If the exception is common, compiler requirements must be captured for further processing catch; abnormal, if capture is then discarded and the runtime + finally mop handle, catch or captured for further processing together.

As plus finally, there is the matter did not capture "mop-up" to handle exceptions, should be carried out.

 

77. try-catch-finally, if in return the catch, finally it will be implemented?

A: Yes execution, execution before the return.

Code Example 1:

/ * 
 * The Java interview questions - if there catch a return statement, finally will execute the code inside it? 
 * / 
Public class FinallyDemo2 { 
    public static void main (String [] args) { 
        System.out.println (the getInt ()); 
    } 

    public static int the getInt () { 
        int A = 10; 
        the try { 
            System.out.println (A / 0); 
            a = 20 is; 
        } the catch (an ArithmeticException E) { 
            a = 30; 
            return a; 
            / * 
             * when a return to the program execution of this step, but this is not return a return 30; this return path is formed a 
             * but then, it is found that there is finally back, so the contents of the finally proceed, a = 40  
             * to return to the previous path, continue to go return 30, after formation of the return path, here's a not a variable, while It is a constant 30
             * / 
        } finally { 
            a = 40;
        }

//      return a;
    }
}

The results: 30

Code Example 2:

 1 package com.java_02;
 2 
 3 /*
 4  * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 5  */
 6 public class FinallyDemo2 {
 7     public static void main(String[] args) {
 8         System.out.println(getInt());
 9     }
10 
11     public static int getInt() {
12         int a = 10;
13         try {
14             System.out.println(a / 0);
15             a = 20;
 16          } the catch (an ArithmeticException E) {
 . 17              a = 30 ;
 18 is              return a;
 . 19              / * 
20 is               * when a return to the program execution of this step, but this is not return a return 30; this return path is formed
 21               * but then, it is found that there is finally back, so finally is content to continue, a = 40
 22               * Back to the previous path again and continue to go return 30, after formation of the return path, here's a not a variable, while is a constant 30
 23 is               * / 
24          } the finally {
 25              a = 40 ;
 26 is              return a; // if so, to again form a return path, since only return through a return, so a direct return 40 here 
27         }
28 
29 //      return a;
30     }
31 }

执行结果:40

 

78. 常见的异常类有哪些?

  • NullPointerException:当应用程序试图访问空对象时,则抛出该异常。

  • SQLException:提供关于数据库访问错误或其他错误信息的异常。

  • IndexOutOfBoundsException:指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。 

  • NumberFormatException:当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常。

  • FileNotFoundException:当试图打开指定路径名表示的文件失败时,抛出此异常。

  • IOException:当发生某种I/O异常时,抛出此异常。此类是失败或中断的I/O操作生成的异常的通用类。

  • ClassCastException:当试图将对象强制转换为不是实例的子类时,抛出该异常。

  • ArrayStoreException:试图将错误类型的对象存储到一个对象数组时抛出的异常。

  • IllegalArgumentException:抛出的异常表明向方法传递了一个不合法或不正确的参数。

  • ArithmeticException:当出现异常的运算条件时,抛出此异常。例如,一个整数“除以零”时,抛出此类的一个实例。 

  • NegativeArraySizeException:如果应用程序试图创建大小为负的数组,则抛出该异常。

  • NoSuchMethodException:无法找到某一特定方法时,抛出该异常。

  • SecurityException:由安全管理器抛出的异常,指示存在安全侵犯。

  • UnsupportedOperationException:当不支持请求的操作时,抛出该异常。

  • RuntimeExceptionRuntimeException: those abnormal super class may be thrown during the normal operation of the Java Virtual Machine.

Guess you like

Origin www.cnblogs.com/java1024/p/11422323.html