CoreJava learning 6 -- Exception Handling

Source: Internet
Author: User

I. Java Exception Handling Mechanism


1. Exception class


The Throwable class is defined in Java Exception handling, and Exception and Error are two child classes derived from it.


Error: errors in JRE can't be solved by programmers. We cannot solve these errors through code. For example, the memory resource is exhausted.


Exception: Indicates exceptions caused by network faults, file damage, device errors, and invalid user input.


The purpose of exception handling is to properly terminate the program when an exception occurs to avoid catastrophic consequences, including;


The notification notifies the user of the occurrence of an exception.


Restore and save important data, and close file rollback transactions.


Exit the program in the appropriate form.


Java uses try-catch to capture and solve exceptions.


2. try statement


Try {...} specifies a piece of code, which is a code judgment that captures and processes the exception range and may cause errors ).


Try {...} can exist independently. try {...} is usually followed by one or more catch blocks. catch is used to catch exceptions and solve such exceptions.


During execution, try {...} code may throw one or more exception objects, and catch will handle these exceptions respectively.


If no exception is generated, the catch will be ignored and not executed.


3. catch statements


Exceptions declared in catch encapsulate the information about Exception events. In catch, you can use some methods of this object to obtain the information.


For example:


The getMessage () method is used to obtain information about the exception event.


The printStackTrace () method is used to trace the stack content when an exception event occurs.


String [] arrays = null;

Try {

System. out. println (arrays. length );

} Catch (NullPointerException e ){

System. out. println (e. getMessage ());

}

System. out. println ("END ");


The exception output is asynchronous.


4. throw statement


We can throw an exception on our own. For example, after the Exception Processing is complete, we can throw the exception to capture the exception processing block at the next layer.


For example, throw new ArithmeticException ();


String [] arrays = null;

Try {

System. out. println (arrays. length );

} Catch (NullPointerException e ){

Throw (e );

}

System. out. println ("END ");


4. throws Declaration


Many methods will be set in the Program), this Method may cause exceptions due to some errors, but you do not want to directly handle these exceptions in this Method, if you want to call this method for the same processing, you can use throws to declare that this method will throw an exception.


Example: public static void stringToDate (String str) throws ParseException {}


Generally, a related exception should be thrown directly when the method is declared.


4. finally


The finally statement block provides a uniform exit for exception handling so that the state of the program can be managed before the control flow is switched to other parts of the program.


No matter whether exceptions are thrown in the program specified by try, the Code specified by finally must be executed.


In finally, resources are usually eliminated, such as closing open files and deleting temporary files.


It is generally written after catch or try.


5. RuntimeException


Java exceptions can be divided into detecting exceptions, non-detecting exceptions, and custom exceptions.


RuntimeException is a non-checkable exception. during compilation, the thrown methods defined in the Declaration are ignored.


When a RuntimeException and its subclass are thrown in a method, you do not need to declare this type of exception throw in the method.


Common exceptions:


NullPointerException NULL pointer exception


NumberFormatException numeric format exception


ArrayIndexOutOfBoundsException


ClassCastException styling exception


6. Exception Handling During method Rewriting


When there are multiple catch statements, the exception size is usually related, and the large types are processed later.


If some throws exceptions are declared in a method of the parent class when inheritance is used, and this method is redefined in the subclass class, you can:


If the exception is not handled, throws are not set during definition.


Only some exceptions defined in the parent class are thrown.


A subclass exception that throws an exception defined by the parent class.


But not:


Throws has an additional exception ).


The parent class exception of the exception declared by the throws parent class method.


-----


Throw throws an exception. When this statement is encountered in the method, the method ends and an exception is thrown. Therefore, do not run any statements that will inevitably be executed after throw; otherwise, the compilation is incorrect.


This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1295065

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.