Java know how much (53) Use Java to create your own exception subclasses

Source: Internet
Author: User
Tags stack trace java throws

Although Java's built-in exceptions handle most common errors, you might 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 don't need to actually do anything-their presence in the type system allows you to use them as exceptions.

The exception class itself does not define any method. Of course, it inherits some of the methods provided by Throwable. As a result, all exceptions, including those you create, can be obtained by means of the throwable definition. These methods are shown in table 10-3. You can also override one or more of these methods in the exception class that you create.

Table 10-3 methods defined by Throwable
Method Description
Throwable Fillinstacktrace () Returns a Throwable object that contains the full stack trace, which may be raised again.
String Getlocalizedmessage () Returns the local description of an exception
String GetMessage () Returns the description of an exception
void Printstacktrace () Show Stack traces
void Printstacktrace (Printstreamstream) To send a stack trace to a specified stream
void Printstacktrace (Printwriterstream) To send a stack trace to a specified stream
String toString () Returns a String object that contains the description of the exception. When a Throwable object is output, the method is called by println ()


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

1 //This program creates a custom exception type.2 classMyExceptionextendsException {3     Private intdetail;4MyException (inta) {5Detail =A;6     }7 8      PublicString toString () {9         return"myexception[" + detail + "]";Ten     } One } A  - classExceptiondemo { -     Static voidComputeintAthrowsMyException { theSYSTEM.OUT.PRINTLN ("called COMPUTE (" + A + ")"); -        if(A > 10) -           Throw NewMyException (a); -System.out.println ("Normal exit"); +     } -  +      Public Static voidMain (String args[]) { A        Try { atCompute (1); -Compute (20); -}Catch(MyException e) { -System.out.println ("Caught" +e); -         } -     } in}

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


The main () method sets an exception handler for MyException and then calls Compute () with a valid value and an illegal value to display the different paths that execute the code. Here's the result:
Called COMPUTE (1)
Normal exit
Called COMPUTE (20)
Caught myexception[20]

Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

Java knows how much (in) the nesting of Try statements

Java know how much (a) throw: Exception throws

Java know how many () Java throws clauses

Java knows how many (or) finally

Java know how much (52) built-in exceptions

Java know how much (53) Use Java to create your own exception subclasses

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.