Learning Java for android-Exceptions and learningandroid

Source: Internet
Author: User

Learning Java for android-Exceptions and learningandroid

  Exceptions

Java allows us to create our own exception classes, but before we create them, we should first ask ourselves if the exception classes that come with jdk really cannot meet our own needs. If any, we should use the exception class that comes with jdk. When others read the code, they are usually familiar with jdk exception systems and are unfamiliar with the exception classes you write. If you need to create an Exception class by yourself, consider whether the Exception class we wrote should inherit Exception or RuntimeException. Generally, the latter is inherited.

Suggestions for exception handling:

  There are a few additional items to keep in mind when working with throws clses and throw statements:

  1. You can append a throws clause to a constructor and throw an exception from the constructor when something goes wrong while the constructor is executing. The resulting object will not be created.
2. when an exception is thrown out of an application's main () method, the virtual machine terminates the application and CILS the exception's printStackTrace () method to print, to the console, the sequence of nested method cballs that was awaiting completion when the exception was thrown.
3. if a superclass method declares a throws clause, the overriding subclass method does not have to declare a throws clause. however, if it does declare a throws clause, the clause must not include the names of exception classes that are not also encoded in the superclass method's throws clause.
4. A checked exception class name does not need to appear in a throws clause when the name of its superclass appears.
5. The compiler reports an error when a method throws a checked exception and does not also handle the exception or list the exception in its throws clause.
6. do not include the names of unchecked exception classes in a throws clause. these names are not required because such exceptions shocould never occur. furthermore, they only clutter source code, and possibly confuse someone who is trying to understand that code.
7. You can declare a checked exception class name in a method's throws clause without throwing an instance of this class from the method. Perhaps the method has yet to be fully coded.
For the second point, the younger brother doesn't understand well.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.