Java Exception handling Error

Source: Internet
Author: User

Java Exception handling Error

The best time to find errors is in the compile phase, which is before you run the program. However, no errors can be identified during compilation, and the remaining issues must be resolved at run time. This requires an error source to pass the appropriate information to a recipient in a way that knows how to handle the problem.

The purpose of exception handling in Java is to simplify the generation of large, reliable programs by using less than the current amount of code, and this way you can make yourself more confident: There are no unhandled errors in your app.

The word "exception" has my meaning to be surprised by it. The problem arises, you may not know how to deal with it, but you do realize that you should not ignore it; you have to stop and see if someone else is somewhere else that can handle the problem. There is just not enough information in the current environment to solve this problem, so submit the question to a higher level environment where you will make the right decision.

another pretty obvious benefit of using exceptions is that it tends to reduce the complexity of error-handling code. If you do not use an exception, you must check for a specific error and handle it in many parts of the program. If you use an exception, you do not have to check at the method call because the exception mechanism will ensure that the error is caught. And, just one place to handle the error, the so-called exception handler. This approach not only saves code, but also separates the code that describes what to do during normal execution and what to do with the problem.

Process of exception handling:

When an exception is thrown, there are a few things that can happen with it. First, as with the creation of other objects in Java, the exception object is created on the heap using new. The current execution path is then terminated, and a reference to the exception object is ejected from the current environment. At this point, the exception handling mechanism takes over the program and starts looking for an appropriate place to proceed with the execution of the program. The proper place is the exception handler, whose task is to recover the program from an error state so that the program can either run in a different way or continue running.

Import java.util.*;p ublic 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:

The finally clause is always executed, regardless of whether the exception is thrown.

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;}}}}


Java Exception handling Error

Related Article

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.