Java Exception Handling interview questions, java exception handling questions
1. What are the differences between error and exception?
An error indicates a system-level error. It is an internal error in the java Runtime Environment or a hardware problem. You cannot expect a program to handle this problem. You have no choice but to exit the operation. It is thrown by the java Virtual Machine.
Exception indicates the exceptions that the program needs to capture and handle. Exceptions are caused by imperfect program design and must be handled by the program.
2. What is the difference between a running exception and a general exception?
Java provides two main types of exceptions: runtimeException and checkedException.
General exceptions (checkedException) mainly refer to IO exceptions and SQL exceptions. For such exceptions, JVM requires that we must perform cathc processing on them. Therefore, whether or not we want to handle such exceptions
All write a lot of catch blocks to handle possible exceptions.
We generally do not handle runtimeException. When such exceptions occur, the program will be taken over by the virtual machine. For example, we have never handled NullPointerException, and
This exception is one of the most common ones.
When an exception occurs during running, the program will throw the exception up until it encounters the processing code. If no catch block is found for processing, it will reach the top level. If it is multi-Thread, there will be Thread. run () throw, such
If it is not multithreading, It is thrown by main. run. After a thread is thrown, the thread is terminated. If it is the main program, the program is terminated.
In fact, exceptions during runtime are also inherited from exceptions. You can also use catch blocks to handle them, but we generally do not handle them. That is to say, if a catch Exception is not processed during runtime, the result is not
The main program is terminated when the thread exits.
If you do not want to terminate, We must capture all possible runtime exceptions. If an exception occurs in the program, but it does not affect the execution of the following program, we should change it in the catch Block.
Often discard data, and then record the log. If it affects the program running below, it is better to exit the program.
3. Principles of Exception Handling Mechanism in Java
Java handles exceptions in an object-oriented way. Java classifies exceptions by different types and provides good interfaces. In Java, every exception is an object and it is Throwable.
Or its subclass instance. When a method encounters an exception, an exception object is thrown. The object contains the exception information. The method that calls this object can capture the exception and handle the exception. Java
Exception Handling is implemented through five keywords: try catch throw throws finally.
Generally, we use try to execute a program. If an exception occurs, the system throws it. We can capture it by its type, or finally, the default processor processes it (finally ).
Try: Specifies a program to prevent all exceptions.
Catch: Followed by a try to catch exceptions.
Throw: Used to explicitly throw an exception
Throws: Used to indicate various exceptions that a member function may throw.
Finally: make sure that a piece of code is executed no matter what exceptions occur.
4. How do you handle exceptions in projects.
(1) Avoid runtimeException. For example, for code that may contain null pointers, before using an object, you must determine whether the object is null. If necessary
Try catch.
(2) When performing try catch, you must record the exception information in the catch code block, obtain the exception information by calling the relevant methods of the exception class, and return it to the web end, not only should we provide good services to users
The user experience should also help programmers well locate the location and cause of exceptions. For example, in a previous project, the program encounters an exception and the page displays an image to tell users which operations cause the program to appear.
And there is a button on the picture to show the details of the exception to the programmer.
5. Differences between final, finally, and finalize
(1) If final is used to declare variables, methods, and classes, it means that the variable values are unchangeable, methods cannot be overwritten, and classes cannot be inherited.
(2) finally is a keyword in exception handling, indicating that the code in finally {} must be executed.
(3) finalize is a method of the Object class. This method is called during garbage collection.
6. If there is a return statement in try (), will the code in finally {} be executed? When will it be executed, before or after return?
I wrote a code and tested it:
public class finallyDemo
public static void main(String[] args) {int i=getInt();System.out.println(i);}private static int getInt() {// TODO Auto-generated method stubtry {return 0;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{return 1;}}}
If the output result is 1, remember it. You don't want to drill the tip of the question, and there is no big use.
Java Exception Handling knowledge
The two are format exceptions. java classifies them as java. lang. NumberFormatException.
Therefore:
Try {
Int number = Integer. parseInt (str );
} Catch (NumberFormatException e ){
E. printStackTrace ();
}
Java interview questions
List and Set all inherit the Collection interface, while Map does not. List, Set does not contain key-value pairs, that is, the data storage method is generally list. add (value); set. add (value); While Map contains key-value pairs, key names can be defined, such as map. put ("key", value ).
The List elements can be repeated, the elements in the Set cannot be repeated, or the Map can be repeated.
List is linear, Set is not sorted, and Map is not sorted.
ArrayList: List implemented by arrays at the underlying layer features: High query efficiency, low addition/deletion efficiency, low lightweight thread security
Slow List: List implemented by two-way circular linked List at the underlying layer features: Low query efficiency and high addition and deletion Efficiency
Throws an Exception for a function, usually following the method, such as: public void function () throws Exception {}
Throws exceptions can be caught in try {} catch (Exception e) {} to output Exception information.
Throw throws an exception. You can manually throw an exception at a specified position based on the custom needs of developers. It is usually written in the function body.
The try {} function is an internal transaction to be captured. It is usually used in conjunction with catch. As an exception handling, finally is the final execution method. No matter whether an exception occurs, finally internal code will be executed.
Java can use try... catch... finally to capture exceptions, for example:
/** Catch Exception test method **/public void testCatch () {try {testThrow (); // catch and throw an Exception in try catch} catch (Exception ex) {ex. printStack (); // The exception information captured by the console output System. out. println ("Exception" + e. getMessage (); // e. getMessage is the exception summary} finally {System. our. println ("Finally"); // execute this command whether or not an exception is thrown.}/** Test Method for throwing an exception **/public void testThrow () throws Exception {new throw Exception ("Custom throw Exception ");}