Java Getting Started notes 5_ exceptions

Source: Internet
Author: User
Tags exception handling throwable
1. Exception/Error
1.1 Basic structure of exception handling
The basic structure of exception handling is as follows:
try {
Somereallyexceptionalmethod ();
catch (nullpointerexception N) {//a subclass of RuntimeException
. . .
catch (RuntimeException R) {//a subclass of Exception
. . .
catch (IOException i) {//a subclass of Exception
. . .
catch (myfirstexception m) {//our subclass of Exception
. . .
catch (Exception e) {//a subclass of Throwable
. . .
catch (Throwable t) {
...//Errors, plus anything not caught above are caught
Finally {...}
The exception and error classes in Java are inherited from the Throwable class, that is, the Throwable class has two subclasses: the error class and the exception class, and an instance of the error class is an internal error in the Java Runtime Environment, which is very few and very fatal, We cannot or rarely handle these errors, or catch them.
And the exception class can be divided into two classes, one is Run-time exceptions (Runtime exceptions) such as Securityexception,arrayindexoutofbounds, NullPointerException, the other is otherwise, such as eofexception. In the exception class hierarchy, the higher the upper exception class, the more general, and the more exception the more targeted. While most exception classes are part of the Java.lang package, others exist in other packages.
In general, it is possible to put code that may be wrong into a try block and catch a possible error in a catch, usually starting with a specific error, always throwable, and the code that needs to be processed in the finally if any errors occur, such as exit loops, releasing resources, and so on.
1.2 Declaring methods that might produce an exception
When declaring a method, you can add a keyword throws to indicate that the method may produce some or some exceptions, such as:
public boolean Myothermethod (int x, int y)
Throws Anexception, Anotherexeption, athirdexception {
...}
With such a declaration, it is best to use Try...catch to handle the exception specified by the method.
1.3 Create your own exception class
The exception class that you define should inherit from other exception classes, preferably a close exception class to inherit, such as exception classes that define file format errors, preferably inherited from IOException. If a close exception class inheritance is not found, it is inherited directly from the exception class, because it is the originator of all exception classes, and the following is a simple custom exception class:

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.