Java fundamentalism--Exception and error difference error and Exception

Source: Internet
Author: User
Tags throwable
Exception and error difference error and Exception
Learn the difference between the exception and the error, and know what to do when you intercept an exception.
by Josh Street

Many programmers do not realize that there is a difference between an error and an exception, and that this distinction has important implications for how you manipulate your code when a problem arises (see sidebar, " Profile Errors and exceptions"). As Mary Campione in the Java Tutorial (Java Guide), "an exception is an event that occurs during the execution of a program that interrupts the operation of normal instructions." "According to American Heritage Dictionary, an error is" an action or an instance that deviates from the acceptable code behavior. "

So what's the difference between deviations (deviation) and interruptions (disruption)? We can explain this: if you are driving on a road and someone has stopped you, this is the interruption. If the car doesn't start, it's a deviation (unless it's my car, we think it's normal).

what does this have to do with Java? Have a lot of relationship. Java has a very interesting error and exception hierarchy (see figure 1).

Indeed, all the code using try {} catch (Exception e) {} can only find half of your errors. But whether you should intercept throwable depends on what you're going to do with it once you've intercepted it. A quick understanding of the subset of the error allows you to know the names of many classes, such as Virtualmachineerror, Threaddeath, and Linkageerror. When you intend to intercept these errors, be sure that you are dealing with them because they are serious problems and therefore are wrong.

Figure 1.
But ClassCastException is not a mistake. Not really. A classcastexception--or an exception-just a way for a VM to inform you that, in this way, the VM lets you know that you (the developer) have made a mistake and now has a chance to modify it.

On the other hand, an error is a failure of the VM (although it can be any system-level service). Let's quote Javadoc's definition of error: "Error is a subset of Throwable, which refers to a serious problem that a reasonable application cannot intercept." Most of them are anomalies. ”

Therefore, errors are difficult to handle, and the average developer (not you, of course) cannot understand the subtleties of dealing with these errors. So what do you do in your job when you think you're going to have an event that is enough to be called a mistake?

First, remember that errors are thrown like exceptions, only a little different. A method that throws an error does not need to declare what it is doing (in other words, the exception is unchecked):

public void Myfirstmethod () throws Exception
    //since it ' s a Exception, I have to declare//it in the throws 
    e {
    throw new Exception ();
}

public void Mysecondmethod ()
    //because errors aren ' t supposed to occur, you//don ' t have to declare 
    them. 
{
    throw new Error ();
}

Note that several exceptions are unchecked, and therefore behave as if they were errors: NullPointerException, Both ClassCastException and Indexoutofboundsexception are runtimeexception subclasses, and runtimeexception and all subsets are usually unchecked.

So what should you do with these pesky unchecked anomalies? You can intercept anomalies in the way they may appear, but this approach is quite accidental. This solves a problem, but it causes other unchecked to interrupt other parts of the code. We should thank the threadgroup for a very good solution:

 public class Applicationloader extends Threadgroup {private Applicationloader (
     ) {Super ("Applicationloader");
               public static void Main (string[] args) {Runnable appstarter = new Runnable () { public void Run () {//invoke your application (i.e. MYSYSTEM.M
     Ain (args)}} new Thread (New Applicationloader (), Appstarter). Start (); }//we overload this is from our parent//threadgroup, which'll make sure that it//gets called  n it needs to be.
This is//where the magic occurs.
          public void uncaughtexception (thread thread, Throwable exception) {//handle the error/exception.
     Typical operations might is displaying a//useful dialog, writing to an event log, etc. }

This method has made a great difference to our programming. Think about it, in the past when you perform an operation in your GUI, if a unchecked exception occurs, your GUI is usually in an abnormal state (the dialog box is still open, the button is not active, the pointer is in an error state), but using this method, You can get the GUI back to its normal state, informing the user of the error, and you'll feel good about it because you've written a high quality application.

But this technique is not just for the GUI. Server applications that use too much resources can use this method to release resources globally, often avoiding a VM entering an unstable state. It is the difference between a great programmer and an ordinary programmer to intercept errors as early and as much as possible and to deal with them in a sensible way. As you have read this article, it is obvious what kind of programmer you want to be.


About the Author:
Josh Street is an architect of Bank of America, who is primarily responsible for developing e-business solutions. His contact method is rjstreet@computer.org.

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.