Exceptions in Java

Source: Internet
Author: User

As a program developer, we have all seen exceptions thrown after the code is run. Beginners See the program after the operation did not appear the expected results, but appeared to see the abnormal hints, the heart will be broken. There is a word circulating among programmers, no code without exception. Although more absolute, but enough to see the exception in the code development process, the exception is how common.

Of course, the problem to solve is the attitude we should be, if we shrink, it is really "Java from the beginning to give up."

In Java, exceptions are objects that are produced in a program's run, and they have various classes to differentiate and also have their own property behavior. The description of the red font we see in the console is the type of exception that the object calls its own method to describe where it occurred. After the exception object is generated, it needs to be thrown before it can be recognized by the Java Virtual machine. This is especially true when we need to be aware of our custom exceptions!

Exception propagation mechanism: If the program is running, a called method produces an exception and he does not handle it, then he will throw this exception to the method called it, and out of the method call stack, if the method called it is still not processed, then the exception will continue to throw to call it the method, The method also jumps out of the method call stack, if until the main method is not resolved, the main method will tell the exception to the Java virtual machine, and out of the method call stack, the Java Virtual machine does not solve the exception of the ability of the program to end the run. Note that before the method jumps out of the call stack, if some code is already running, the result of the code running does not go away.

So the question is, how do we deal with a method that has the potential to produce an exception?

First of all, we should try to avoid the occurrence of anomalies, such as in the input, limit the scope of user input, and all possible input conditions to judge, the different input situation to deal with;

Second, we can use the Try-catch-finally code block to handle the exception, the code contained in the try{} is a block of statements that may have an exception, also known as protected Code, in catch () {}, the parentheses are the exception type names that need not be caught, and the exception object names. In curly braces for our processing statements, if you catch an exception that satisfies the parentheses, regardless of the extra code in the curly braces, the exception is assumed to be resolved, the program continues to run, and the code that executes in finally{} is enforced regardless of whether the exception is caught or not, and if try{} is in the block, break/ Return, and so on, will also be executed in the finally end of the statement, that is, as long as entering the try code block, if there is finallly will be executed.

Furthermore, only if the method is likely to produce an exception that he cannot solve himself, then it throws an exception to the caller. However, it is often passive for developers to throw exceptions when run by methods, especially when the code is likely to be read and rewritten by others, and it is difficult for others to take into account all possible situations. At this point, the throws keyword appears, which is used in the declaration of the method, with the throws keyword appended to the method name and its parentheses, plus the exception type name thrown, which completely represents the exception that a method might throw. In this case, the party caller must use the TRY-CATCH statement block to handle or use throws again to declare the exception to be thrown. And so on Note here that the caller's declared exception type can only be the same exception type that was thrown by the caller or its parent class, otherwise the compilation will report an error. The Declaration method of the compile-time exception above, and for the runtime exception, if the caller does not declare the compilation will not be wrong, but as a developer, we should also deepen a statement or use Try-catch to deal with.

The above is my understanding of the exception in Java, and the processing method, and the details will be added later.

  

  

  

Exceptions in Java

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.