A little understanding of the anomaly

Source: Internet
Author: User

The largest class in the exception is Throwable, which is divided into two subclasses: Exception,error

Exception: Is an exception that the program can handle

Error: Indicates JVM errors, general program cannot handle

The difference between exception and runtimeexception:

1. If the type of exception is thrown, it must be processed using Try...catch.

2. If the type of runtimeexception is thrown, it is not necessary to use try...catch for processing, and once an exception occurs, it will be handled by the JVM, but in order to ensure the health of the program, it is recommended to use a try if there is a possibility of an exception ... catch to handle.

Throws the difference between the keyword and the throw keyword:

1. When defining a method, you can use the throws keyword declaration, which uses the throws declaration method to indicate that this method does not handle the exception, and that it is handled at the call of the method.

2. You can use the Throw keyword to artificially throw an exception, throwing an instantiated object that throws the exception class directly

Custom exception Classes

Custom exception classes can be completed only by inheriting exception, and you can customize the exception class when you need to define the exception information you want.

Example:

Class MyException extends exception{//Custom exception class, inheriting Exception class
Public myexception (String message) {//constructor method receives exception information
Super (message); Calling a construction method in a parent class
}
}
public class Defaultexception {
public static void Main (string[] args) {
try {
throw new MyException ("Custom Exception");
} catch (Exception e) {
E.printstacktrace ();
}
}
}

A little understanding of the anomaly

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.