Java exception architecture diagram and several interview questions, java architecture diagram questions

Source: Internet
Author: User

Java exception architecture diagram and several interview questions, java architecture diagram questions

1. java exception Architecture

The pink ones are checked exceptions, which must be captured by the try {} catch Block or declared by the throws clause in the method signature. the CHecked Exception must be captured and processed during compilation. The name is CHecked Exception because the Java compiler needs to check and the Java Virtual Machine needs to check to ensure that this rule is followed.
A green exception is a runtime exception. The programmer needs to analyze the code to determine whether to capture and process the exception, such as a null pointer...
If it is declared as Error, it is a serious Error and must be specially processed based on the business information. The Error does not need to be captured.

2. If there is a return statement in try {}, will the code in finally {} following this try be executed? When will it be executed, before or after return?

Maybe your answer is before return, but to be more detailed, my answer is to execute in the middle of return. Please refer to the running result of the following program code:

 1 public  classTest {   2    3     public static void main(String[] args) {   4        System.out.println(newTest().test());   5     }   6     staticint test()   7     {   8        int x = 1;   9        try  10        {  11            return x;  12        }  13        finally  14        {  15            ++x;  16        }  17     }  18 }  

--------- Execution result ---------

The running result is 1. Why? The process in which the main function calls the subfunction and obtains the result is like preparing an empty jar for the primary function. When the subfunction returns the result, it first places the result in the jar, then return the program logic to the main function. The so-called return is the sub-function. If I don't run it, you can continue to run the main function. There is no result. The result is put into the jar before you say this.

3.Differences between final, finally, and finalize.

Final is used to declare attributes. Methods and classes indicate that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited.

Internal classes need to access local variables. Local variables must be defined as final types, for example, a piece of code ......


Finally is a part of the structure of the exception handling statement, indicating that it is always executed.


Finalize is a method of the Object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file. JVM does not guarantee that this method is always called

4.Write the five most common runtime exceptions.

This question mainly tests how large your code volume is. If you write code for a long time, you will often see system exceptions, you don't have to answer five specific system exceptions, but you have to be able to say what a system exception is and how many system exceptions are. Of course, these exceptions are best written using their English names. If they cannot be written, use Chinese!

The so-called system exception ....., They are all subclasses of RuntimeException. in jdk doc, you can view the list of RuntimeException classes, that is, all system exceptions. I was impressed by the following system exceptions: NullPointerException, ArrayIndexOutOfBoundsException, and ClassCastException.

5.How to handle exceptions in JAVA? What are the meanings of the keywords throws, throw, try, catch, and finally? Can I throw an exception in the try block?

Java uses object-oriented methods to handle exceptions, classify various exceptions, and provides good interfaces. In Java, each exception is an object, which is an instance of the Throwable class or other subclass. 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 it. Java exception handling is implemented through five keywords: try, catch, throw, throws, and finally. I

Generally, try is used to execute a program. If an exception occurs, the system throws an exception. In this case, you can catch it by its type, or finally (finally) is processed by the default processor;

Try is used to specify a program to prevent all "exceptions;

The catch clause follows the try block to specify the type of the "exception" you want to capture;

Throw statements are used to explicitly throw an "exception ";

Throws is used to indicate various "exceptions" that a member function may throw ";

Finally ensures that a piece of code is executed no matter what "exception" occurs;

You can write a try statement outside a member function call, and write another try statement inside the member function to protect other code. Every time a try statement is encountered, the framework of "exception" is placed on the stack until all try statements are completed. If the try statement at the next level does not process an "exception", the stack will be opened until a try statement that handles this "exception" is encountered.

6.What are the differences between runtime exceptions and general exceptions?

An exception indicates an abnormal state that may occur during the running of the program. An exception indicates an exception that may occur during common operations on the virtual machine. It is a common running error. The java compiler requires that methods must declare and throw possible non-runtime exceptions, but do not require that they throw uncaptured runtime exceptions.

7.. What is the output result of the following program code?

Public class smallT {

Public static void main (String args []) {

SmallT t = new smallT ();

Int B = t. get ();

System. out. println (B );


Public int get ()


Try {return 1 ;}

Finally {return 2 ;}


The returned result is 2.

I can use the following example program to explain this answer. From the running result of the example below, we can find that the return statement in the try statement calls the function before the function called in finally, that is to say, the return Statement is executed first, and the finally statement is executed later. Therefore, the returned result is 2. Return is not to let the function return immediately, but to put the returned result into the function stack after the Return statement is executed. At this time, the function does not return immediately, it must execute the finally statement before returning.

You can use the following program to analyze the answer:

Public classTest {

Public static voidMain (String [] args ){

System.Out. Println (NewTest (). test ());;


IntTest ()


Try {return func1 ();}

Finally {return func2 ();}


IntFunc1 ()


System.Out. Println ("func1 ");



IntFunc2 ()


System.Out. Println ("func2 ");




----------- Execution result -----------------




Conclusion: The finally code is executed after the return and break statements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.