Java basic interview questions and answers (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 trash. 

 

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
  * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
  */
public  class  FinallyDemo2 {
     public  static  void  main(String[] args) {
         System.out.println(getInt());
     }
 
     public  static  int  getInt() {
         int  a =  10 ;
         try  {
             System.out.println(a /  0 );
             a =  20 ;
         catch  (ArithmeticException e) {
             a =  30 ;
             return  a;
             /*
              * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
              * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
              * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
              */
         finally  {
             a =  40 ;
         }
 
//      return a;
     }
}

The results: 30

Code Example 2:

Package Penalty for com.java_02 1; 
 2 
 3 / * 
 4 * the Java interview questions - if there catch a return statement, finally will execute the code inside it? 
 *. 5 / 
 . 6 {public class FinallyDemo2 
 . 7 public static void main (String [] args) { 
 . 8 System.out.println (the getInt ()); 
 . 9} 
10 
. 11 the getInt public static int () { 
12 is A = int 10; 
13 is {the try 
14 System.out.println (A / 0); 
15 = 20 is A; 
16} the catch (an ArithmeticException E) { 
. 17 = 30 A; 
18 is A return; 
. 19 / *  
20 is * a return to the execution of this step time, but this is not return a return 30; this return path is formed
21 is * but then, it was found and there are finally so finally proceed to the content of, a = 40
* 22 to return to the previous path, continue to go return 30, after forming the return path, where a is not a variable, but rather constant 30 
23 is * / 
24} {the finally 
25 a = 40; 
26 is a return; // If so, it is again formed a return path, since only return through a return, so a direct return 40 here 
27} 
28 
29 a // return; 
30} 
31 is}

The results: 40

 

78. What are the common exception classes?

  • NullPointerException: When an application tries to access an empty object, Thrown.

  • SQLException: providing an exception database access error or other error messages about.

  • IndexOutOfBoundsException: Throws an index indicating a sort (e.g. sorted array, a string, or a vector) is out of range. 

  • NumberFormatException: when the application tries to convert a string into a numeric type, but that the string can not be converted into the appropriate format, the thrown exception.

  • FileNotFoundException: When you try to open the file specified pathname fails, throw this exception.

  • IOException: When some kind of I / O exception This exception is thrown. Such a failure or interruption of I / O operations generated generic class exception.

  • ClassCastException: When attempting to cast an object is not an instance of a subclass Thrown.

  • ArrayStoreException: trying to store the wrong type to an object thrown when an array of anomalies.

  • IllegalArgumentException: Thrown to indicate that passed an illegal or incorrect parameter to the method.

  • ArithmeticException: when an exceptional arithmetic condition occurs, Thrown. For example, an integer "divide by zero" throws an instance of this class. 

  • NegativeArraySizeException: If an application tries to create an array with negative size, Thrown.

  • NoSuchMethodException: a particular method can not be found, the Thrown.

  • SecurityException: Thrown by the security manager to indicate a security violation.

  • UnsupportedOperationException: When the requested operation is not supported Thrown.

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

Guess you like

Origin www.cnblogs.com/donleo123/p/11621049.html