Java Core API--9 (Exception)

Source: Internet
Author: User
Tags finally block

Exception handling

The parent class in the exception structure Throwable class, its Exceptionlei class and the error class. What we can catch in the program is the subclass exception of exception.

Error at System level: Error in Java Runtime Environment, we are not controllable.

Exception is a program-level error: we can control it.

1) catch Exceptions in two ways:

one is to add Try-catch to catch the exception, and the other is to declare the exception thrown (continue to throw out) in our method.

      2) exception-handling statement: Try-catch, if a try block catches an exception, it is processed in the catch block, otherwise skipping the catch block is skipped (development, there must be a solution to write, can not be resolved to throw up throws); Good programming habits, the last written catch (Exception e) (parent class, top exception) of the exception capture mechanism captures unknown errors (or no need for processing errors); catch is from top to bottom, so do not write the parent exception on the subclass exception, otherwise the subclass exception will never have the opportunity to deal with! You can use methods in a catch block to get exception information: ① GetMessage () Method: Used to get information about an exception event. ②printstacktrace () Method: Used to track the contents of the execution stack when an exception event occurs.

try{//keyword, only one try statement

code snippet with possible exception

}catch (IOException e) {//Enumerate the types of exceptions that may occur in the Code

when an enumerated exception type is present, it is handled here, and the targeted processing

} catch (Exception e) {//Enumerate the types of exceptions that may occur in the Code

when an enumerated exception type is present, it is handled here, and the targeted processing

     }

3) throws Keyword: You do not want to handle the exception directly in a method, but instead want the caller to handle the exception uniformly. When declaring a method, we can declare the type of exception that may be thrown at the same time, notifying the caller to force the capture. is the so-called "ugly words in front." In principle throws the exception declared, be sure to throw it in the method. Otherwise it doesn't make sense. Conversely, if we actively throw an exception through throw in the method, we should declare that kind of exception in throws and notify the outside world of the catch. Note: You cannot throws on the main method because the caller JVM shuts down the program directly.

4) Exceptions are thrown in Java: When a Java Virtual machine runs a program, but an error occurs while a line of code is running, the JVM creates an instance of the error and throws it. The JVM then checks if the method in which the error code is located has a try capture, and if so, checks if the catch block has the ability to handle the exception (see if you can pass the exception instance as a parameter to see if there is a matching exception type). If not, the exception is thrown to the caller of the method (throwing up). And so on, until the main method is left unresolved (that is, it is thrown to the JVM processing). Then the JVM terminates the program.

5) Exception exception in Java is divided into:

① Non-detection exception (RuntimeException subclass): No exception is checked at compile time. If the class exception or its subclasses are thrown in a method, the exception that is thrown by the class may not be enumerated in throws when the method is declared. Common run-time exceptions are: NullPointerException, illegalargumentexception, classcastexception, NumberFormatException,

ArrayIndexOutOfBoundsException, ArithmeticException

② detects exceptions (non-runtimeexception subclasses): Compile-time checks, except for exceptions at run-time exceptions, which can be checked for exceptions, you must declare the method with a throws declaration of the type of exception that might be thrown!

6) Finally block: The finally block is defined at the end of the catch block (all catch last), and can only occur once (0-1 times), regardless of whether the program is an error will be executed block! Unconditional Execution! The elimination of resources is usually done in the finally statement, such as closing open files, deleting temporary files, and so on.

public static void Main (string[] args) {

SYSTEM.OUT.PRINTLN (test (NULL) + "," +test ("0") + "," +test (""));

}

/** output results? 1,0,2? 4,4,4 for correct results */

public static int test (String str) {

try{

Return Str.charat (0)-' 0 ';

}catch (NullPointerException e) {

return 1;

}catch (RuntimeException e) {

return 2;

}catch (Exception e)

return 3;

}finally{

return 4; //Unconditional execution

}             

}

7) exception handling When overriding a method

If inheritance is used, some throws exceptions are declared somewhere in the parent category, and when the method is redefined in a subcategory, you can: ① do not handle the exception (throws is not set when redefined). ② can only throws some exceptions (one or several) on methods that are redefined in the parent category. ③ can throws the subclass of the exception on the redefined method (the subclass that throws the exception).

However, you cannot: ①throws out extra exceptions. ②throws the parent category of the exception on the method that was redefined (the parent class that threw the exception).

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1670623

Java Core API--9 (Exception)

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.