Java anomaly frame composition and several side questions

Source: Internet
Author: User

1.java Exception schema diagram

The pink is the checked exception (checked exceptions), which must be captured by the Try{}catch statement block or declared in the method signature through the throws clause. The checked exception must be caught at compile time, named checked Exception Because the Java compiler is checking, the Java Virtual machine also checks to ensure that the rule is adhered to.
The green exception is the runtime exception (runtime exceptions), which requires the programmer to analyze the code to determine whether to capture and process, such as a null pointer, 0 apart ...
When declared as error, it is a serious error and requires special handling based on business information, and error does not need to be captured.

There is a return statement in 2.try{}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return?

Perhaps your answer is before return, but to be more detailed, my answer is to execute in the middle of return, see the following program code running results:

1  PublicClasstest {2   3      Public Static voidMain (string[] args) {4System. out. println (Newtest (). Test ()); 5     }  6 staticint Test ()7     {  8        intx =1; 9        Try  Ten        {   One            returnx;  A        }   -        finally   -        {   the++x;  -        }   -     }   -}

---------Execution Results---------

The running result is 1, why? The main function calls the sub-function and obtains the result the procedure, like the main function prepares an empty jar, when the child function returns the result, first puts the result in the jar, then returns the program logic to the main function. The so-called return, that is, the sub-function said, I do not run, your main function to continue to run it, there is no result, the result is to say this before put into the jar.

3. final, finally, finalize the difference.

Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.

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

Finally is part of the exception-handling statement structure, which indicates that it is always executed.

Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file. The JVM does not guarantee that this method is always called

4. please write down the 5 runtime exception you are most familiar with.

This question mainly test your code amount is how big, if you long-term write code, should often see some system aspects of anomalies, you do not have to answer 5 specific system exceptions, but you have to be able to say what is the system anomaly, and a few system exceptions can be, of course, These exceptions are completely written in their English name is the best, if it is not written, then use Chinese bar, there is better than no strong!

The so-called system anomaly, that is ..., they are runtimeexception subclasses, in the JDK doc in the RuntimeException class, you can see all its subclasses list, that is to see all the system exceptions. I am more impressed with the system anomalies are: NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException.

5.How does the Java language perform exception handling, keywords: what does throws,throw,try,catch,finally mean? Can I throw an exception in a try block?

Java uses an object-oriented approach to exception handling, classifies various exceptions, and provides a good interface. In Java, each exception is an object, which is an instance of the Throwable class or other subclass. When an exception is thrown, a method throws an exception object that contains the exception information, and the method that invokes the object can catch the exception and handle it. Java exception handling is achieved through 5 keywords: try, catch, throw, throws, and finally. One

The case is a try to execute a program, if an exception occurs, the system throws (throws) an exception, then you can use its type to catch (catch) it, or finally (finally) by the default processor to handle;

A try is used to designate a piece of program that prevents all "exceptions";

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

The throw statement is used to explicitly throw an "exception";

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

Finally, a piece of code is executed to ensure that a piece of code occurs regardless of "exception";

You can write a try statement outside of a member function call and write another try statement inside this member function to protect the other code. Whenever a try statement is encountered, the "exception" frame is placed on the stack until all the try statements are completed. If the next-level try statement does not handle an "exception", the stack expands until it encounters a try statement that handles this "exception."

6. what are the similarities and differences between runtime exceptions and general exceptions?

An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error. The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.

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

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 help me explain this answer, from the running results of the following example can be found that the return statement in the try call function is executed before the function called in Finally, that is, the return statement executed first, the finally statement after the execution, so, The returned result is 2. Return does not return the function immediately, but the return statement executes, and the returned result is placed in the function stack, and the function does not return immediately, and it does not actually begin to return until the finally statement is executed.

You can use the following procedure to help analyze the answer:

Public class Test {

Public static void Main (String[]args) {

System. out. println (new test (). Test ());

}

int Test ()

{

try {return func1 ();}

Finally {return Func2 ();}

}

int func1 ()

{

System. out. println ("func1");

return 1;

}

int Func2 ()

{

System. out. println ("Func2");

return 2;

}

}

-----------Execution Results-----------------

Func1

Func2

2

Conclusion: The code in finally executes after the return and break statements

Java anomaly frame composition and several side questions

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: info-contact@alibabacloud.com 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.