Java Exception Handling

Source: Internet
Author: User
Tags try catch

Java Exception Handling

All abnormal classes in java are inherited and Throwable classes, which mainly include the Error class and Exception class.

The Error class includes Virtual Machine errors and thread deadlocks, known as program Terminator;

Exception classes mainly refer to encoding, environment, and user operation input except for problems. They mainly include non-check exceptions (RuntimeException) and check exceptions (CheckException ).

 

 

 

 

 

 

Exception Handling:

1. try-catch-finally

Try: capture exceptions. Once an exception is found in try, the control of the program will be perceived to the exception handler in the catch statement block. Try statements cannot exist independently and must be in the same village as catch or finally statement blocks;

Catch: the handler. Such as issuing warnings, prompts, Checking configuration, network connection, recording errors, etc. After the catch Block is executed, the program jumps out of the catch Block and continues to execute the subsequent code;

Finally: The final code that is run before the method is called after the result is executed by try and catch. It is used to close and release resources.

Try {// some thrown exceptions} catch (Exception e) {// code block for processing the Exception} finally {// code to be executed} terminate the execution, by the exception handler (throwing a reminder or logging), the code outside the Exception Code block is executed normally. Try throws many types of exceptions, and multiple catch blocks catch multiple clock errors. Multi-Exception Processing code block sequence problem: first subclass and then parent class (an error will also be prompted if the order is incorrect), finally statement block processing will eventually execute the code

 

Throws is placed after the parameter, and multiple exceptions can be thrown before the method body. Each exception is separated by a comma;

Throws is written in the method body. If a method calls a method that throws an exception, you must add a try catch statement to catch the exception or add a declaration to throw the exception to the caller at the previous layer for processing.

You can customize exceptions based on the Exception class or its subclass.

 

Exception chain:

(1): e. printStrackTrace (); it is used to perform a deep-level output exception call Process (2): RunTimeException (); this is a runtime exception, is the parent class that can be thrown exceptions during normal operations of all Java virtual machines! (3): newExc. initCause (e); this is a packaging technique for exceptions. InitCause () is used to save the original exceptions. When you want to know what exceptions occur at the underlying layer, call getCause () to obtain the original exceptions.

 

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.