Java-based custom exception classes and the difference between throw and throws

Source: Internet
Author: User

First, the exception of the schema:

Throwable class: So the exception class is a subclass of Throwable, which derives two subclass error and exception.

The error class: Indicates that only by the program itself can not recover the serious errors, such as memory overflow, virtual machine errors, and so on, in addition to trying to make the program safe exit outside, we have no way to solve, so the development of the time we should pay more attention to the exception

Exception class: Non-critical errors thrown and handled by Java applications, such as files not found, network connections interrupted, array subscript out of bounds, and its different subclasses correspond to different types of exceptions

Run-time exceptions: include the RuntimeException class and its subclasses, do not require programmers to handle them, if the program has a runtime exception, and we do not process, will output the stack information of the exception, and terminate the program run

Checked exception (non-runtime exception): The program must catch or declare the exception class that is inherited by exception except for runtime exceptions. Otherwise the program cannot compile through, such as IOException, SQLException and other exceptions

Ii. steps to customize the exception:

When the type of exception provided by the JDK does not meet the requirements of the program, we can customize the exception

1. Define a generic class and inherit exception or Runtimeexceprion, the custom exception that inherits Exceprtion is the checked exception (must throw or try Cacth), Custom exception classes that inherit from Runtimeexceprion are run-time exceptions

2. Write the constructor of the exception class, and inherit the parent class implementation, common as follows:

/*** Custom Exception classes*/ Public classMyExceptionextendsException { Publicmyexception () {} Publicmyexception (String message) {Super(message); }    /**     *      * @parammessage needs to display the exception information *@paramcause is an instance of Throwable or its subclasses*/     Publicmyexception (String message, throwable cause) {Super(message, cause); }     Publicmyexception (Throwable cause) {Super(cause); }}
Custom exception classes that inherit from exception
    @Test    publicvoidthrows  Exception {        try  {             int a=  10/0;         Catch (Exception e) {            throw  new myexception ("10 cannot be divided by 0");}    }
tests for exception classes that inherit from exception

In addition to inheriting from exception can also inherit runtimeexception, specific examples will not write down

Three, the difference between throw and throws

The main differences are the following three points:

1. Different functions: Throw is used to throw an exception in the program; throws is used to throw an exception within the method;

2, the use of different locations: Throw in the method body, can be used as a separate statement; throws must be followed by the method parameter list and cannot be used alone;

3, content is not allowed: Throw throws an exception object, and can only be one; throws is followed by an exception class, and can be followed by multiple exception classes

Java-based custom exception classes and the difference between throw and throws

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.