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.