Detailed Java built-in exceptions and methods for creating custom exception subclasses _java

Source: Internet
Author: User
Tags exception handling throwable

Built-in exception subclass

In the standard package Java.lang, Java defines a number of exception classes. Some of these have been used in the previous example. These exceptions are typically subclasses of the standard class runtimeexception. Because Java.lang is actually introduced by all Java programs, most of the exceptions derived from RuntimeException are automatically available. Furthermore, they do not need to be included in the throws list of any method. In the Java language, this is called an unchecked exception (unchecked exceptions). Because the compiler does not check it to see if a method handles or throws these exceptions. The unchecked exceptions defined in Java.lang are listed in table 1. Table 2 lists the exceptions defined by Java.lang that must be included in the throws list of methods, if they can produce one of these exceptions but cannot handle it on their own. These are called checked exceptions (checked exceptions). Java defines several other exception types that are associated with libraries that are not the same.

Unchecked exception subclasses defined in the Java.lang of table 1:java

Check exceptions defined in table 2:java.lang

Use Java to create your own exception subclasses

Despite most common errors in Java's built-in exception handling, you may want to create your own exception types to handle the special situations you apply. This is very simple: just define a subclass of exception (exception is, of course, a subclass of Throwable). Your subclasses do not need to actually perform anything-their presence in the type system allows you to use them as exceptions.

The exception class does not define any methods of its own. Of course, it inherits some of the methods provided by Throwable. As a result, all exceptions, including those you create, can get throwable-defined methods. These methods are shown in table 3. You can also override one or more of these methods in the exception class that you create.
Table 3 methods defined by Throwable

The following example declares a new subclass of exception, which is then used as a signal to the error situation in the method. It overloads the ToString () method so that the description of the exception can be displayed with println ().

This program creates a custom exception type.
Class MyException extends Exception {
  private int detail;
  MyException (int a) {
    detail = A;
  }

  Public String toString () {return
    "myexception[" + detail + "]";
  }
}

Class Exceptiondemo {
  static void compute (int a) throws MyException {
    System.out.println ("called COMPUTE (" + A + ")");
    if (a >)
     throw new MyException (a);
    System.out.println ("Normal exit");
  }

  public static void Main (String args[]) {
    try {
      compute (1);
      Compute (a);
    catch (MyException e) {
      System.out.println ("caught" + E);}}}


This example defines a subclass of the exception myexception. The subclass is very simple: it contains only a constructor and an overloaded ToString () method that displays an exception value. The Exceptiondemo class defines a compute () method. This method throws a MyException object. This exception is thrown when the integer parameter of compute () is greater than 10.

The main () method sets up an exception handler for MyException and then calls Compute () with a valid value and an illegal value to display the different paths that execute through the code. Here is the result:

Called COMPUTE (1)
Normal exit
called COMPUTE (a)
caught myexception[20]

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.