Understand the difference between Error and Exception

zhaozj2021-02-16  50

Understand the difference between Error and Exception

Understand the difference between Error and Exception, learn how to process it

Many programmers are unclear between ERROR and Exception, which is very important for how to handle problems correctly (see 1, "brief narrative error and exception"). Just like Mary Campione's "The Java Tutorial": "Exception is an incident that occurs in the execution of the normal instruction stream (an Exception is an Event this Occurs During the Execution of a Program That Disrupts the Normal Flow of instructions. "In accordance with the American traditional dictionary (American Heritage Dictionary), Error is:" The Act OR AN Instance of Deviarating from an affilt code of behavior.) "

Different (disruption), what is the difference? Let us think so: If you drive on the road, someone stops you, this is called "interrupt". If the car can't be launched at all, it is called "departure".

What is the relationship with Java? a lot of. There is a quite interesting ERROR and Exception of Java.

Yes, very correct: All code blocks using try {} catches {} can only find half of your errors. However, whether Try and catch throwable depends on the reason you capture it. Look at Error's subclasses, their names Similar to VirtualMachineError, ThreadDeath, LinkageError. When you want to capture these guys, you have to make sure you need to capture them. Because those are very serious errors.

But is CLASSCASTEXCEPTION a ERROR? not completely. ClassCastException or any type of Exception is only a virtual machine (VM, VirtualMachine) let you know the way you have problems, this shows that developers have an error, and now there is a chance to correct it.

On the other hand, Error is a problem with a virtual machine (usually this, but may also be an operating system problem). Quote in the Java documentation About Error: "Error is the subclass of throwable, which has a serious problem. General application unless there is reason, it should not capture Error. Usually this is very abnormal."

So, Error is very powerful, but it is difficult to deal with it than the general developers are imagined (of course not you). If you are doing a very simple job, do you think it is necessary to deal with Error?

First, remember that Error is substantially identical to the way of Exception, only one point. It is a way to throw Error does not need to declare this (in other words, this is an unchecked exception (also known as Runtime Exception).

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 some Exception is uncontrollable, the same as Error's performance, such as: NullPointerException, ClassCastException, and IndexOutOfboundsexception, all of which is the subclass of RuntimeException. RuntimeException and its subclasses are Unchecked Excception.

How should I deal with these annoying unchecked Exception? You can catch them in places where you may have problems, but this is just an incomplete solution. This solves a problem, but the rest of the code may still be interrupted by other unchecked Exception. Here is a better way, thank the ThreadGroup class provides this great way:

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 skill is great. Think about this situation, you have modified your GUI program, then a unchecked exception is thrown. And your GUI program often has a state of error (the dialog is still open, the button is invalid, and the cursor state has an error). However, using this trick, you can restore your GUI program and notify the user to have an error. It feels great to you, because you have written a high quality app.

This trick does not only apply to the GUI program. Server-side applications can use this trick to release resources to prevent virtual machines from entering instability. Earlier capture errors and intelligent processing is a good programmer and one of the differences between ordinary programmers. You have already understood this, I know which type of programmer you want to be.

About author

Josh Street is Bank of America architect designer. He is responsible for the development of e-commerce platforms. His contact information is RJSTREET@computer.org. Attachment

Summary narrative error and Exception

Error and Exception are inherited from throwable, they have the following points:

EXCEPTIONS

1. Can be controlled or uncontrollable (unchecked)

2. Indicates a mistake caused by programmers

3. Should be processed at the application level

Errors

1. Always uncontrollable (unchecked)

2. Not used to use errors for system errors or low-level resources

3. How can I have possible words, should be captured in the system level

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

New Post(0)