Learning---exception handling in Java learning

Source: Internet
Author: User

Basic Knowledge

Any computer programming language includes absolutely correct and relatively correct statements. Absolutely correct: In any case, the program will be executed correctly according to the process; relative correctness: The operation of the program is restricted by the running environment, in which case, additional detection and control statements are required to ensure the robustness of the program.

In Java, an object-oriented approach is used to handle program errors, using an exception (Exception) processing mechanism to handle errors in program execution. All errors in a program run are treated as an exception, and all exceptions in a program are collected to be processed in a section of the program by detecting the block of statements. If an exception occurs, the method generates an object representing the exception (with details of the exception) and gives it to the runtime system, which looks for the appropriate code to handle the exception. The process of generating an exception object and submitting it to the runtime system is called a discard (throw) exception. The runtime system looks in the call stack of the method, starting with the method that generated the exception, until it finds a method that contains the appropriate exception handling, a process known as a catch (catch) exception.

There is an illegal operation in the program. This error caused by unintentional or careless is called an implicit exception, and the common implicit exception can be handled by the subclass error of the Throwable in the Java.lang package. The exception that is caused by programmers using the throw language in the program. This pre-considered exception is known as an explicit exception and can be generated by a subclass exception derived from the Throwable subclass of the Java.lang package.

in the Java language, most errors and exceptions can be thrown. Exception cases are classified as Exception class exception and error classerrors, where the error class represents a serious error that is handled directly by the Java System. Errors and exceptions are categorized into 3 categories:

L Input Error: Mainly refers to the data format entered by the user, not entered in the specified form

L device errors are unpredictable and unavoidable

L Error in program code is due to incomplete consideration when designing and writing

Java exception Handling is achieved by 3 key words: try-catch-finally. Use a try to execute a program, if an exception occurs, the system throws (throws) an exception, it can be caught by its type (catch) and processed, or finally (finally) by the default processor to handle.

1 try{2 Exception_method;3}Catch(Exception1 e) {4 dealWith1;5}[Catch(Exception2 e) {6 dealWIth2;7}][finally{8 method;9 }]Ten when the exception object is thrown, use the two keywords: throws and throw, where throw is used to throw the exception object in the statement, for example: OneException e =NewException ("MyException"); A Throwe; - or - Throw NewException ("MyException"); the throws is used to describe the type of exception that is thrown indirectly.  -  Public voidMyException (intAintbthrowsexception{ -}
View Code

Learning---exception handling in Java learning

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.