Overview:
The exception mechanism in Java is a good thing. But a good thing must be used correctly, or it will make us know it in error. In the wrong way, it will be used incorrectly. This is a vicious cycle. This is not what we would like to see. Do not assume that we have been able to use the exception well, here is a partial question to explain. In this part of the problem, some are from the "effective Java" This book, part of the development process from my usual encountered.
1. Is it a throw or a try-catch ?
This is a problem for people who have just come into contact with programming development and often face but choose bad.
Since we are developing projects that are not as easy as writing demos, there may be many hierarchies here. Where do we want to use the Try-catch exception, or throw the exception to the upper layer? Here, the first thing we need to know is what happens to try-catch and throw.
Try-catch: captures an exception condition and aborts subsequent operations in the try block . And no more throws up the exception.
Throw: when throwing an exception with throw, the current execution block (method) ends the subsequent execution. It is equivalent to a return operation and guarantees that the upper layer can catch the exception at the time of the call and handle it accordingly.
The demo sample is as follows:
public class Exceptionclient {public static void Main (string[] args) { exceptionclient client = new Exceptionclie NT (); Client.showinfo (); } private void Showinfo () { try { System.out.println ("first info"); TestException (); System.out.println ("second info"); } catch (Exception e) { System.err.println (e); } SYSTEM.OUT.PRINTLN ("outside info"); } private void TestException () throws Aexception { Boolean f = true; if (f) { throw new aexception ("Aexception"); } System.out.println ("F is false.");} }
according to the above analysis of try-catch and throw, you can know that the second sentence in the Showinfo method try block is not printed, and the last sentence of the TestException method is not printed. The results are as follows:
Figure-1 Try-catch test Results
2. Whether to use the exception that is being inspected or not
First, we need to know what is the check exception and non-check exception, but as the name implies, the subject is checked. Because the current IDE is smart, the IDE gives an error when we use the exception that is being inspected without try-catch the exception. As follows:
Figure 2 IDE checks for exceptions to be inspected
Non-inspected exceptions are not recognized by the IDE. One more thing, because the IDE detects an exception before it is detected, so if we force this code to run, it is not compiled, and the non-checked exception does not.
Well, the difference between the use of abnormal and non-inspected exceptions is explained. Now let's talk about how to create these different exceptions.
When we are writing a custom exception A.java, the class of a needs to inherit Exception, not the exception B.java is inherited runtimeexception.
Due to the process of using the exception, the developer is forcibly restricted to Try-catch. While Try-catch this exception, the developer can fix the exception and redo the previous operation (that is, recovery). There is no such restriction in runtimeexception. So, when we try to tell the caller that the current exception can be fixed and allow the call to be re-invoked, we use the check exception, and when we think of it as a program error, we need to use a non-check exception.
You might have some basic understanding of when to use an exception or a non-examined exception, and then you might ask a question like: do we have another error, so what's the difference between a bug and an exception? Here's a list of the differences between the two (click here to see the reference source):
Exception:
1. Can be controllable (checked) or uncontrolled (unchecked).
2. Represents an error caused by a programmer.
3. Should be handled at the application level.
Error:
1. Always uncontrollable (unchecked).
2. Often used to represent errors in system errors or low-level resources.
3. If possible, it should be captured at the system level.
3. Use exceptions only for incorrect conditions
In this regard, we should first understand that Java in the exception check when the system resources consumed, than the normal program calls higher. Then, if our program is constantly checking for exceptions, it will have a very small effect on the overall performance of the program. We can see this in a small example. As follows:
Assuming that the list of 10 million elements is present, we will traverse this list in three ways, as follows:
the first: an anomaly check for each of these cases
private void Call_1 (list<integer> List) { Long T = System.currenttimemillis (); try { int index = 0; while (true) { list.get (index++); } } catch (Indexoutofboundsexception e) { logutils.printtimeused (" Not for check exception ", t); } }
The second type: Exception checking only for the wrong situation
private void Call_2 (list<integer> List) { Long T = System.currenttimemillis (); t = System.currenttimemillis (); int size = List.size (); int index = 0; while (true) { if (index >= size) { try { list.get (index++); } catch (Indexoutofboundsexception e) { C9/>logutils.printtimeused ("Targeted check exception", t); break; } } List.get (index++); } }
the third type: normal loop traversal
private void Call_3 (list<integer> List) { Long T = System.currenttimemillis (); t = System.currenttimemillis (); int size = List.size (); int index = 0; for (index = 0; index < size; index++) { list.get (index++); } Logutils.printtimeused ("loop traversal", t); }
Test Results:
Figure-3 different exception checking methods traverse List
From the test results above, we can see that the exception is not checked against the ground (blindly checking the exception), which is much lower than the targeted check of abnormal performance. So, when we use exceptions, take extra care. Need to avoid some unnecessary anomaly checks to optimize our program code.
Ref:
"Effective Java"
Java: How to correctly use exceptions in detail