Exceptions in java and c #

xiaoxiao2021-03-06  40

Too much clue, finishing ... In Java, Exception is divided into check, unchecked. For Checked Exception, if a method throws Checked Exception inside, you must declare it in the method signature; and other methods of calling this method, you must handle this Checked Xception, or you must redeept and throw the same EXCEPTION. For Unchecked Exception, you don't need to declare it. In C #, all Exception does not need to be specifically declared, it seems to be unchecked exception.

For the issue of the use of check or uncheck exception of, Sun on their course had this to say: "Because the Java language does not require methods to catch or specify runtime exceptions, it's tempting for programmers to write code that throws only runtime exceptions or to make all of their exception subclasses inherit from RuntimeException. Both of these programming shortcuts allow programmers to write Java code without bothering with all of the nagging errors from the compiler and without bothering to specify or catch any exceptions. While this may seem convenient to the programmer, it sidesteps the intent of Java's catch or specify requirement and can cause problems for the programmers using your classes. Checked exceptions represent useful information about the operation of a legally specified request that the caller may have had no control over and that the caller needs TO BE INFORMED ABOUT - for Example, The File System Is Now Full, or The Remote End Has Closed The Connection, or The Access Privileges Don't Allow this Acti on. What does it buy you if you throw a RuntimeException or create a subclass of RuntimeException just because you do not want to deal with specifying it? Simply, you get the ability to throw an exception without specifying that you do so. In other Words, IT IS A WAY TO AVOID Documenting The Exceptions That a Method Can throw. When I this Good? WELL, WHEN Is It Ever Good To Avoid Documenting a Method's Behavior? The answer is hardly ever.

These rules have been widely accepted, but there are many people now put forward the objection, think that it should be more widely used unchecked Exception. Their reasons are: 1. Checked Exception is often inappropriate exposure method for implementation details. For example, there is a way to query user information, but often throw SQLException or oException when it can't find a specific user, not a special meaning NosuchuseRexception. In his book Effective Java, 43 had this to say: "Throw exceptions appropriate to the abstraction, In other words, exceptions thrown by a method should be defined at an abstraction level consistent with what the method does, not necessarily with the low -level details of how it is implemented. For example, a method that loads resources from files, databases, or JNDI should throw some sort of ResourceNotFound exception when it can not find a resource (generally using exception chaining to preserve the underlying cause), rather THE THE LOWER-Level IOException, SQLException, or Namingexception. "So, throw a meaningful Exception, and will indicate that the specific underlying problem is added to the Exception Stack for debugging is a better way. 2. CHECKED Exception will result in instability stabilized. This is a significant problem, but this problem can also be solved with 43 to solve this problem with an Exception abstraction to a certain level. 3. Checked Exception causes the readability of the code to decline. Imagine if there is only two lines of code, but it has to handle 5 or 6 Exception will be a case. 4. Article 3 will also know that some programmers use a Catch (Exception EX) statement to process all Exceptions. The biggest problem with Unchecked Exception is if the document can't keep up, people who use these methods are a big trouble. I don't know what this method will throw some abnormalities.

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

New Post(0)