Java exception handling error

Source: Internet
Author: User

Java exception handling error
Java exception handling error

The best time to discover errors is in the compilation phase, that is, before you run the program. However, you cannot identify all errors during compilation, and the remaining problems must be solved in the running stage. This requires the error source to pass the appropriate information to a receiver in some way, and the receiver knows how to handle the problem.

The purpose of exception handling in Java is to simplify the generation of large and reliable programs by using less than the current number of code, and this method can make you more confident: there are no unprocessed errors in your application.

The word "exception" has an unexpected meaning. When the problem arises, you may not know how to handle it, but you do know you should not ignore it. You have to stop and check if someone else is there to handle it. However, there is not enough information in the current environment to solve this problem. Therefore, submit the problem to a higher-level environment and make a correct decision here.

Another obvious benefit of exceptions is that they can reduce the complexity of error handling code. If exceptions are not used, you must check specific errors and handle them in many places of the program. If an exception is used, you do not need to check the method call, because the exception mechanism can ensure that this error is captured. In addition, you only need to handle errors in one place, that is, the so-called exception handling program. This method not only saves code, but also separates the code that describes what to do during normal execution from the code that describes what to do when a problem occurs.

Exception Handling process:

When an exception is thrown, several events occur. First, like creating other objects in Java, new will be used to create exception objects on the stack. Then, the current execution path is terminated and the reference to the exception object is displayed from the current environment. At this point, the exception handling mechanism takes over the program and starts to find an appropriate place to continue executing the program. The proper part is the exception handling program. Its task is to restore the program from the error state so that the program can either run in another way or continue to run.

import java.util.*;public class WhoCalled{static void f(){try{throw new Exception();}catch(Exception e){for(StackTraceElement s:e.getStackTrace())System.out.println(s.getMethodName());}}static void g(){f();}static void h(){g();}public static void main(String[] args){f();System.out.println("--------------------------------------");g();System.out.println("--------------------------------------");h();}}

Finally clause:

Whether or not an exception is thrown, the finally clause can always be executed.

import java.util.*;class ThreeException extends Exception{}public class FinallyWorks{static int count=0;public static void main(String[] args){while(true){try{if(count++==0)throw new ThreeException();System.out.println("No exception");}catch(ThreeException e){System.out.println("ThreeException");}finally{System.out.println("In finally clause");if(count==2)break;}}}}


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.