Java exception Surface question

Source: Internet
Author: User
Tags throwable

There is a return statement in 1.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:

 public   classtest { public  static  void   main (string[] args) {System.  out     .println (Newtest (). Test ()); } staticint Test () { int  x = 1 try   {x  ++;        return   X;   finally   { ++x; }    }}

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

2

The running result is 1, why? The main function calls the child function and obtains the result the procedure, the main function opens up a new space in the memory, when the child function returns the result, first puts the result in the new space, then returns the program logic to the main function. The so-called return, 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 the new space in the sub-.

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

 Public classsmallt{ Public Static voidMain (String args[]) {smallt T=Newsmallt (); intb = 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  classTest { Public Static voidMain (String[]args) {System. out. println (Newtest (). Test ());; }    intTest () {Try{returnA1 ();} finally{returnA2 ();} }       intfunc1 () {System. out. println ("A1"); return 1; }    intFunc2 () {System. out. println ("A2"); return 2; }  }

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

A1

A2

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

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. 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.

5. What is the difference between error and exception?

Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations.

Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.

6. The simple principle and application of exception handling mechanism in Java.

Exceptions are abnormal conditions or errors that occur when a Java program is run (not compiled), similar to real-life events, where real-life events can contain information about the time, place, people, and plot of an event, which can be represented by an object, and Java uses an object-oriented approach to handling exceptions. It encapsulates each exception that occurs in the program into an object that contains information about the exception.

Java classifies exceptions, different types of exceptions are represented by different Java classes, and the root class for all exceptions is java.lang.throwable,throwable two sub-classes are derived: Error and Exception,error Represents a serious problem that the application itself cannot overcome and recover, and the program has only a dead copy, such as a system problem such as memory overflow and thread deadlock. Exception said that the program can also overcome and restore the problem, which is divided into system anomalies and common anomalies, system anomalies are the defects of the software itself caused by problems, that is, the software developers to consider the problem caused by poor, software users can not overcome and restore this problem, In this case, however, the software system can continue to run or let the software die, for example, array scripting out of bounds (arrayindexoutofboundsexception), null pointer exception (NULLPOINTEREXCEPTION), Class Conversion Exception (classcastexception), common exception is the operation of the environment changes or anomalies caused by the problem, is the user can overcome the problem, for example, network disconnection, hard disk space is not enough, after such an exception, the program should not die.

Java provides a different solution for system exceptions and common exceptions, and the compiler enforces common exceptions that must try: Catch processing or using the throws declaration continues to be thrown to the upper call method processing, so the common exception is also called the checked exception, and the system exception can be handled or not handled, so the compiler does not enforce with try. catch processing or declaration with throws, so system exceptions are also known as unchecked exceptions.

Prompt answer: According to three levels to think: the virtual machine must be down the error, the program can die or can not die of errors, the program should not die error;

7, the most common to the 5 runtime exception.

NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException.

8.JAVA language How to do exception processing, keyword: throws,throw,try,catch,finally what is the meaning of each? 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";

Java exception Surface question

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.