1. throw and throws What's the difference?
throws is used to declare all the exception information that may be thrown by a method. throws is to declare the exception but not handle it. Instead, it uploads the exception to the user who calls it. throw refers to a specific exception type thrown.
2. final,finally,finalize What's the difference?
final can modify classes, variables and methods. A modified class indicates that the class cannot be inherited, a modified method indicates that the method cannot be overridden, and a modified variable indicates that the variable is a constant and cannot be re assigned.
Finally is generally used in the try catch code block. When handling exceptions, we usually use the code method finally code block that must be executed, which means that the code block will be executed regardless of whether there is an exception or not. It is generally used to store some code that closes resources.
Finalize is a method belonging to the Object class, and the Object class is the parent class of all classes. This method is generally called by the garbage collector. When we call the gc() method of System, the garbage collector calls finalize() to collect garbage.
3. try-catch-finally Which part of the can be omitted? Answer: catch Can be omitted
reason:
More strictly speaking, try is only suitable for handling runtime exceptions, and try+catch is suitable for handling runtime exceptions + ordinary exceptions. In other words, if you only try to handle ordinary exceptions without catch, the compilation will not pass, because the compiler has a hard rule that if you choose to catch ordinary exceptions, you must use catch to display the declaration for further processing. There is no such provision for runtime exceptions at compile time, so catch can be omitted. You can add catch compiler to it.
In theory, the compiler doesn't like any code and thinks there may be potential problems, so even if you add try to all the code, the code will only add a layer of skin on the basis of normal operation at run time. However, once you add try to a piece of code, you explicitly promise the compiler to catch the exceptions that may be thrown by the code instead of throwing them up. If it is an ordinary exception, the compiler requires that it must be caught with catch for further processing; If the runtime exception is caught, then discarded and + finally swept, or catch caught for further processing.
As for adding finally, it is a "clean-up" process that must be carried out regardless of whether exceptions are caught or not.
4. try-catch-finally Medium, if catch in return Well, finally Will it still be implemented? A: it will be implemented in return Execute before.
Code example 1:
* java Interview questions--If catch There are return sentence, finally Will the code be executed? */ 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 When the program reaches this step, this is not return a, but return 30; This return path is formed * However, it finds that there is finally, so continue to execute the finally content, a=40 * Return to the previous path again and continue to return 30. After forming the return path, a here is not a variable, but a constant 30 */ } finally { a = 40; } // return a; } }
Code example 2: package com.java_02; /* * java Interview question -- if there is a return statement in catch, will the code in finally be executed? */ 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 When the program reaches this step, this is not return a, but return 30; This return path is formed * However, it finds that there is finally, so continue to execute the finally content, a=40 * Return to the previous path again and continue to return 30. After forming the return path, a here is not a variable, but a constant 30 */ } finally { a = 40; return a; //If so, a return path is formed again. Since you can only return through one return, you can directly return 40 here } // return a; } }
-
What are the common exception classes?
NullPointerException: This exception is thrown when an application attempts to access an empty object. SQLException: Provides exceptions about database access errors or other error information. IndexOutOfBoundsException: Indicates a sort index(For example, sorting an array, string, or vector)Thrown when out of range. NumberFormatException: This exception is thrown when an application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format. FileNotFoundException: This exception is thrown when an attempt to open the file represented by the specified pathname fails. IOException: When something happens I/O This exception is thrown when an exception occurs. This class is failed or interrupted I/O The generic class of the exception generated by the operation. ClassCastException: This exception is thrown when an attempt is made to cast an object to a subclass that is not an instance. ArrayStoreException: An exception thrown when trying to store an object of the wrong type into an object array. IllegalArgumentException: An exception thrown indicates that an illegal or incorrect parameter was passed to the method. ArithmeticException: This exception is thrown when an abnormal operation condition occurs. For example, when an integer is "divided by zero", an instance of this class is thrown. NegativeArraySizeException: This exception is thrown if the application attempts to create an array with a negative size. NoSuchMethodException: This exception is thrown when a particular method cannot be found. SecurityException: An exception thrown by the security manager indicating a security violation. UnsupportedOperationException: This exception is thrown when the requested operation is not supported. RuntimeExceptionRuntimeException: Those who might be Java Superclass of the exception thrown during normal operation of the virtual machine.