Java exception: Throwable

Source: Internet
Author: User
Tags getmessage throw exception throwable try catch

I. Overview

1. Throwable interface1) Error class2) Exception class
    • Compile-time issues:
      • It's not RuntimeException's anomaly.
      • Must be processed, because you do not process, the compilation will not pass.
    • Run-time Issues:
      • RuntimeException exception
      • We also do not deal with this problem, because it is your problem, and this problem appears certainly is our code is not rigorous, need to fix the code.

Second, the treatment of abnormal:

(i) Abnormal handling methods1, the JVM default processing: error. The exception name, reason, location and other information output in the console, the program can not continue to execute. 2, self-treatment1) Mode 1:
    • Try...catch...finally: Write your own processing code, the subsequent program can continue to execute.
    • It is not possible to continue execution in the spring transaction, which will be rolled back by default as long as it is runtimeexception.
2) Mode 2:
    • Throws: You can't handle it, declare it on the method, tell the caller, there's a problem.
3. Interview question: What is the difference between a compile-time exception and a run-time exception?
    • Compile-time exception: must be processed, otherwise the compilation does not pass
    • Run-time exception: Can not be processed, can also handle
(ii) Processing of detailed1, try...catch...finally processing (example)
<span style= "FONT-SIZE:18PX;" ><span style= "font-family:arial;" >/* * Multiple exceptions * b: Write a try, multiple catch * try{* ... *}catch (Exception class name variable name) {* ... *} * catch (Exception class name variable name) {* ... *} * ... * *  note Meaning: * 1: can be clearly defined as far as possible, do not use the big to deal with. * 2: The exception of the relationship between the peers who before who does not matter, if there is a child parent relationship, the parent must be in the back. *  NOTE: * Once there is a problem in the try, the problem will be thrown out here, and then to match the problem in the catch, * Once there is a match, execute the catch inside the processing, and then end the Try...catch * continue to execute the following statement. */public void Test3 () {int a = 10;int B = 0;int[] arr = {1, 2, 3};//Grandpa must be in the last try {System.out.println (A/b); System.out.println (Arr[3]); System.out.println ("There's an anomaly here, you don't know who it is, what to do?"); catch (ArithmeticException e) {//brother, there is an exception will not go to another catch System.out.println ("divisor cannot be 0");} catch ( ArrayIndexOutOfBoundsException e) {//Brother System.out.println ("you visited the index of the Unreachable Access");} catch (Exception e) {// Grandpa must be in the back System.out.println ("problem");} System.out.println ("over");//continue to execute}</span></span>

2. New anomalies of JDK71) Format: try{}catch (Exception name 1 | exception Name 2 | ... Variable) {}2) Note
    • The process is consistent. (In actual development, many times may be for the same type of problem, give the same treatment)
    • There must be a peer relationship between multiple exceptions, and there is no way to handle the parent exception.

3. Common methods of anomaly1) public string getMessage (): Exception message string2) Public String toString (): Simple information description of the return exception
    • The name of the class for this object (full path name) ":" (colon and a space)
    • The result of calling this object Getlocalizedmessage () method (the contents of GetMessage () are returned by default)
3) Printstacktrace (): Gets the exception class name and exception information, and the location where the exception appears in the program. return value void. Output information to the console.

4. Throws thrown (on the definition of method)1) Format: Throws exception class name, can throw more than one at a time
    • This format must be followed by the parentheses of the method.
    • public void Test7 () throws ArithmeticException {}
2) Note:
    • The method that calls the method needs to handle the exception, such as not handling, to continue throwing.
3) Summary:
    • The compile-time exception is thrown and must be handled by the caller in the future.
    • The run-time exception is thrown, and future calls are not processed.
5, throw (in the method body)1) Format: Throw exception Object
    • If an exception occurs, we can throw the exception, and this time the thrown object should be an exception.
    • throw new ArithmeticException ();
    • Typically used for custom exceptions
2) Interview questions: The difference between throw and throws
    • Throw
      • In the method body, followed by the exception object name, and can only be one.
      • Throws an exception object, stating that there must be an exception generated here.
    • Throws
      • On a method declaration, followed by the class name of the exception, can be more than one.
      • is to declare that the method has an exception and is a possibility that the exception does not necessarily occur.
(iii) The FINALLY keyword and its face questions1. Finally is used to release resources, and its code will always execute. Special case: The JVM exited before executing to finally2. Interview questions
  • final,finally,finalize the difference?
    • final: The final meaning, can be decorated class, member variable, member method
      • modifier class, class cannot be inherited  
      • modifier variable, variable is constant
      • modifier method cannot be overridden
    • finally: is part of exception handling, Used to release resources.
      • General , the code will definitely execute, special case: The JVM exits the
    • finalize: is a method of the object class for garbage collection.
    • If there is retur in the catch N, will the finally be executed? If executed, before return or after
      • Note: The return path is a problem. *
<span style= "FONT-SIZE:18PX;" ><span style= "font-family:arial;" >/* * Interview questions: * 1:final,finally and Finalize differences * Final: final meaning, can be modified class, member variable, member method * Decorated class, class cannot be inherited * modifier variable, variable is constant * decoration method, method cannot be overridden * final LY: is part of exception handling and is used to release resources. * In general, the code will definitely execute, special cases: the JVM exits before executing to finally * Finalize: is a method of the object class for garbage collection *  2: If the catch has a return statement, May I ask if the code in the finally will be executed? *   If yes, please ask before return, or after return. *    will. Ago. *  * To be  exact, it should be in the middle. *  * 3:try...catch...finally Format variant * a:try...catch...finally * b:try...catch * C:try...catch...catch ... * D:try ... catch...catch...finally * E:try...finally * This practice is currently in order to release resources.  */public int test10 () {int a = 10;try {System.out.println (a/0); a =;} catch (ArithmeticException e) {a = 30;return a;/* * Return a When the program executes to this step, this is not return a but return 30; The return path is formed. * But, it found that there is finally, so continue to execute finally content, a=40 * Again back to the previous return path, continue to walk return 30; */} finally {a = 40;return a;//If this turns out to be 40. }//return A;} </span></span>


(iv) custom exceptions:1) inherit from exception or runtimeexception, only need to provide the non-parametric structure and a parameter structure. 2) Example:
6, abnormal attention to achieve1) The parent's method has an exception thrown, and the child's overriding method must be less than or equal to the parent's exception when throwing an exception2) The parent's method does not throw an exception, and the child's overriding method cannot have an exception thrown3) The parent's method throws more than one exception, and the child's overriding method must be less or smaller than the parent
<span style= "FONT-SIZE:18PX;" ><span style= "font-family:arial;" >/* * Java is unlikely to be considered in all cases, so in actual development we may need to define the exception ourselves. * and our own arbitrary write a class, is not as an exception class, to think that your class is an exception class, you must inherit from exception or RuntimeException * Only need to provide a non-parametric structure and a parameter structure can be. *  * Two ways: * A: Inherit exception, when throw, need to deal with * B: Inherit runtimeexception, run-time exception, at the time of throw, need to deal with *///public class MyException Extends Exception {public class MyException extends RuntimeException {//only need to provide a parameterless construct and a parameter construct. Public MyException () {}public myexception (String message) {super (message);}} </span></span>

iii. Spring Business(i) Business1. Communication behavior: propagation=propagation.required2. Isolation mechanism: Isolation=isolation.default1) Read-only properties: ReadOnly2) rollback mechanism:
    • Rollbackfor
    • Norollbackfor
(b) Interview1, rollback mechanism. 1) Try catch: is not valid because spring's rollback mechanism is rolled out as long as the run-time exception occurs. 2) FIX: Just modify its rollback mechanism.



2) This interview question is often asked (Suzhou). I quilt cover to the company has: Suzhou Cimc, Suzhou Keda (monitoring), Alliance extension and so on.


Java exception: Throwable

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.