Java Keism - Exceptions and Error Difference Error and Exception

xiaoxiao2021-03-06  100

Anomaly and error difference error and exception

Understand the difference between exceptions and errors, and know what should I do when you intercept an exception.

By Josh Street

Many programmers are not realized that an error and an exception are different. When there is a problem, this difference is an important meaning of how to do your code (see toolbar, "brief error and abnormal"). As Mary Campione is written in the Java Tutorial (Java Guide), "an exception is an event that appears during a program execution, which interrupts the running of normal instructions." According to the explanation of American Heritage Dictionary, an error is "Action or an instance of an acceptable code behavior."

So what is the difference between Deviation and Interruption (Disruption)? We can explain this: If you are driving on a road, someone intercepts you, this is interrupted. If the car can't be launched, it is deviation (unless it is my car, we think this is Normal).

What is the relationship with Java? There is a big relationship. Java has a very interesting error and abnormal hierarchy (see Figure 1).

Indeed, all code to use TRY {} catch (Exception E) {} can only find half of your errors. But if you should intercepting throwable depends on how you intercepted it, you are ready to handle it. Quick understanding of the subset of Error allows you to know the names of many classes, such as VirtualMachineError, ThreadDeath, and LinkageError. When you plan to take these errors, it is sure that you have to handle them because they are a serious problem, so it is wrong.

figure 1.

But is CLASSCASTEXCEPTION not a mistake? Not. A classcastexception - or an exception - just a VM (virtual machine) informing you, in this way, VM let you know, you (developer) has made a mistake, now there is a chance to modify it.

On the other hand, the error is a failure of the VM (although it can be any system-level service). Let's reference Javadoc's definitions for Error: "Error is a subset of throwable, which refers to a reasonable application that cannot be intercepted. Most of them are abnormal."

Therefore, the error is hard to handle, the general developer (of course, not you) can't understand the subtlety of these errors. So what should I do when you feel a matter of an event that you feel enough to be called an error?

First, remember that the error is as an abnormality, only a little difference. Throwing a wrong way does not need to declare what it is doing (in other words, unchecked):

Public void myfirstMethod () THROWS EXCEPTION

// Since it's an exception, i Have to Declare

// it in the throws clause {

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 there are several exceptions to unchecked, so their behavior is the same as errors: NullPointersException, ClassCastException, and indexoutofboundsexception are all subclats of RuntimeException, RuntimeException, and all its subsets are usually unchecked.

So how should you deal with these annoying unchecked? You can intercept an abnormality in the methods they may have, but this method has a lot of incident. Do you can solve a problem, but it will make the other parts of the unchecked exceptions. We should thank the ThreadGroup class to provide a good approach:

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.main (args)}

}

New thread (New ApplicationLoader (),

AppStarter) .start ();

}

// We overload this method from Our Parent

// ThreadGroup, Which Will Make Sure That IT

// Gets Called When It Needs to Be. This is IS

// Where the magic occurs.

Public void Uncaughtexception (Thread Thread, Throwable Exception)

{

// Handle the error / exception.

// Typical Operations Might Be Displaying A

// USEful Dialog, Writing to an Event log, etc.

}

This method gives us a big change. Think about it, when you perform an operation in your GUI, if you have an unchecked exception, your GUI is usually in an abnormal state (the dialog is still open, the button cannot be activated, the pointer is in an error ), But use this method, you can return the GUI to its normal state, inform the user, you will feel good, because you have written a high-quality application.

But this trick is not only used for GUI. Server applications with excessive resources can use this method to release resources globally, usually avoiding VM into a unstable state. Intercepted by mistakes as soon as possible, to handle them with a wise method, which is a great programmer and an ordinary programmer. Since you have already read this article, which programmer do you want to be a task is obvious.

About the author: Josh Street is an architect Bank of America, he was responsible for the development of e-commerce solutions. His contact information is RJSTREET@computer.org.

转载请注明原文地址:https://www.9cbs.com/read-123577.html

New Post(0)