Java know how much (43) Exception Handling Basics

Source: Internet
Author: User

A Java exception is an object that describes an exception (that is, an error) that occurs in a code snippet. When an exception occurs, an object representing the exception is created and thrown (throw) in the method that caused the error. The method can choose to handle the exception itself or pass the exception. In both cases, the exception is captured (caught) and processed. Exceptions may be generated by the Java runtime System, or by your manual code. Exceptions thrown by Java are related to basic errors that violate the language specification or exceed the Java execution Environment limit. The exception that is generated by hand coding is basically used to report the error condition of the method calling program.

Java exception handling is controlled by 5 keywords: try, catch, throw, throws, and finally. Here's how they work. The program declares that the exception monitor you want is included in a try block. If an exception occurs in the try block, it is thrown. Your code can catch the exception (with catch) and handle the exception in some reasonable way. System-generated exceptions are automatically thrown by the Java runtime System. Throw an exception manually, throw with the keyword. Any exception to the thrown method must be defined by the throws clause. Any code that is absolutely executed before the method returns is placed in the finally block.

The following is the usual form of an exception handling block:
1 Try {2     //block of code to monitor for errors3 }4 Catch(ExceptionType1 exOb) {5     //exception handler for ExceptionType16 }7 Catch(ExceptionType2 exOb) {8     //exception handler for ExceptionType29 }Ten // ... One finally { A     //block of code to be executed before try block ends -}
Here, Exceptiontype is the type of exception that occurs. Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (43) Exception Handling Basics

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.