Dramond of Java anomalies

xiaoxiao2021-03-06  129

Now I will debate the question, and I should not use "check". If we use "check-type exception", then we have to put this method into the try-catch block when calling an abnormal method, regardless of whether we really want to solve this abnormality here. Drop. A possible way is that if we don't need to handle it, then we can capture the use of the E.FillInstackTack () method after catching it, and maintain an exception to the captured trajectory is not lost. However, it is like this, it is obviously still very troublesome, it is a low efficiency. We have to handle a lot of exceptions every day, it is too much trouble, so many people oppose the use of "inspection-type abnormal". An error that is easily made when using the "inspection-type exception" is to lose anomalies. When we put a method with abnormal declarations in the TRY block, no matter whether we have catch to capture it, or if there is processing, the abnormal handling mechanism will have the correct processing of this exception. Such as: try {} finally {} Try {} catch (exception ex) {// This does not do anything, it is abnormally disappeared here, but it is not effective. } Some people put forward the following packages for "inspection-type exception": class wrapcheckedException {void throwruntimeException () {TROWROW NEW AMYSELFEXCEPTION ("WHERE AM I?");} Catch (Exception E) {// adapt to unchecked: Throw new runtimeException (e);}}} This time we call this method, you don't have to put it in TRY, and the exception will also be captured. When we need to capture it and processes in the CATCH block, you can use the getCause () method, such as: catch (RuntimeException Re) {THROW Re.getcause ();} catch (amyselfexcection e) {E.PrintStackTrace ();} But I think this packaging is really funny. Because the exception thrown in the method is an operational period, we will call this method later, don't know how this method will throw a way, then how will we catch (AmyseelfException E)? Throwings with "inspection-type abnormal" attachment (throws) although there are more code, it is not only an exception to be reported, but we must also make an abnormally according to an abnormality. This special explanation is very useful to us when processing. If it is "non-inspected exception", we don't know what kind of exception we call us. We don't know how this method will throw an exception. In this case, how do we do correctly? We see that the additional instructions for exceptions cannot be canceled. If we cancel this pair of exceptions, then we have to go to the method every time you want to process the abnormality, you have to find the place to throw an abnormally, seeing a specific type of exception. If this method is the method in the third party library, but no explanation of this method will thrown, then you have to ask the third party library manufacturer. At this time, the third party may also need to check the source code, only know what type of exception is thrown in the end.

It can be seen that this will cause many very troublesome consequences. And if we keep an explanation of an exception that will thrive at the end of the method, then it will be easily knowing what kind of exception we want to process will throw. The problem is the current exception handling mechanism. When the method of the abnormality instructions are called each time, this method must be placed in the TRY block, so it is very troublesome. From above, I summed up a comprehensive anomalous treatment program: Since Java's backward compatibility, our existing unusual mechanism cannot be completely shaken. So, we can use the "non-inspected exception" that will be thrown in those methods by adding a clause after the method. In this case, call a method of only the "non-inspection" description clause, we do not need to put it in the TRY block. When we want more specific processing exceptions, we can also put this method into the TRY block and then capture. What exceptions need to be caught in specific? In the "non-inspection" description of the method, is this not very convenient? If the future abnormal processing mechanism has been transformed above, then we have a problem that needs to be proposed. This question is, when do we need to throw "inspection-type exceptions" in the method? There are also a lot of arguments on the Internet. There are many things I have seen, and there are many "inspection-type anomalies" can not be used, and it is a big failure in Java. But I don't think so, but I need a reason to use "check-type anomaly" to give "check-type exception". There is a phenomenon that often happens in our code, but it is very interesting: void foo () {inputStream IN = null; outputstream out = null; try {in = new fileinputstream ("foo1.txt"); out = new fileoutputstream ("foo2.txt"); // do some stuff with in / out} catch (ioException e) {println ("Something Bad Happened");} finally {if (in! = null) {TRY {in .close );} Catch oException (e) {Println ("WHOOOOPS!"); // Very Crazy !!!}} if (out! = Null) {Try {out.close ();} catch (ioException e) {Println ("WHOOOOPS 2!");}}}} In the above code, we see that if the file is not successfully closed, then we also need to write a try block to handle it, but this problem is impossible to get it. In this question, we need it just that the file is not closed, and can only do this. Then we can use a "running period" instead of this "Checked Exception". This example also allows us to see some people say that "the running period is abnormal" is completely unused in Java. Use "Bubbling Up" in "Check-type Exception". For example: I have a f () method, throws exa, exb, exc, and exception.

Often we are lazy to write THROWS Exception, which is very bad. Use "Checked Exception" to bring a lot of complaints. For example: we are in Method F (), but in the method, we don't have THROW unusual, or even there is no need to excente, maybe we don't need it at all, maybe we are in the future. However, when we call the f () method, no matter what we are willing or not, the compiler requires us to put this method into the TRY block, it is really annoyed, and I heard that there is often this in the Sun's class library. The problem exists. Here are the third edition of Thinking in Java:

Exception Guidelines

Use exceptions to:

Handle Problems At The Appropriate Level. (Avoid Catching Exceptions Unless ". Handling exception in a suitable place (avoiding the capture exception in the case where you don't know how to handle it). Fix The Problem and Call The Method That Caused The Exception Again. Solve the problem, then reuse the trigger the problem. Patch Things Up and Continue without Rerying The Method. Correction Problem and then bypass that method will continue. Calculate Some Alternative Result Instead of What The Method WAS Supposed to Produce. Use some other calculations, not the result of that method will return. Do Whatever You CAN in The Current Context and Rethrow The Same Exception To a higher context. Done the current thing you can do, then throw the same exception to a higher level. Do Whatever You CAN in The Current Context and throw a Different Exception to a higher context. Done the current thing you can do, then throw a different exception to a higher layer. Terminate The Program. Abort Process. IF Your Exception Scheme Makes Things More Complicated, The IT IS Painful and Annoying To Use.) Simplified (if your exception makes things more complicated, it will use it very much trouble and pain). Make Your Library and Program Safer. (This Is A Short-Term Investment for Debugging, And A Long-Term Investment for Application Robustness.) Make your class library and program more installed (this is a short-term investment in debugging, is also a procedure Sundays do long-term investment).

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

New Post(0)