Exception handling mechanism and anomaly classification __java in Java

Source: Internet
Author: User
Tags try catch

Java exception handling mechanism: If a method does not complete the task in the normal way, you can exit the method through another path. In this case, an object that encapsulates the error message is thrown. At this point, this method exits at once and does not return any values. In addition, other code that calls this method cannot continue, and the exception handling mechanism will give the code execution to the exception handler. first, the classification of anomalies are as follows:


–>throwable is a superclass of all errors or exceptions in the Java language. The next layer is divided into error and exception
The –>error class refers to the internal error and resource exhaustion errors of the Java Run-time System. The application does not throw the class object. If there is such a mistake, in addition to informing the user, the rest is to try to make the program safely terminate.
–>exception has two branches, one is run-time exception runtimeexception, such as: NullPointerException, classcastexception One is to check for exception checkedexception, such as IOException, SQLException caused by I/O errors.
–>runtimeexception are superclass of exceptions that may be thrown during the normal operation of a Java virtual machine. The exceptions to the derived runtimeexception generally include several aspects:
1. Wrong type conversion
2. Array access bounds
3, Access null pointer
If RuntimeException appears, then it must be a programmer's fault.
–> Check for exception checkedexception is generally an external error, which occurs during the compile phase, and the Java compiler forces the program to catch such exceptions, which will require you to make a try catch of the program that might appear to be abnormal, which generally includes several aspects:
1, trying to read the data at the end of the file
2. Trying to open a URL in the wrong format
3, trying to find the class object according to the given string, and the class that the string represents does not exist two, the exception handles the way:

1, encountered problems without specific treatment, but continue to throw to the caller
There are three forms of throwing exceptions, one is throw, one throws, and the other is a system that automatically throws exceptions.

The program writer knows that a certain code must have an exception and throws public
static void Main (string[] args) { 
    String s = "abc" using the Throw keyword; 
    if (S.equals ("abc")) { 
      throw new numberformatexception (); 
    } else { 
      System.out.println (s); 
    } 
int div (int a,int b) throws exception{return to A/b
The system automatically throws an exception public
static void Main (string[] args) { 
    int a = 5, b =0; 
    System.out.println (5/b); 
}

Attention:
the difference between throw and throws:
1. Different position: throws used in the function, followed by the exception class, can be with multiple, and throw used in the function, followed by the exception object.
2. Different functions: Throws is used to declare an exception, so that the caller only knows the possible problems of the function, can give a prior treatment, throw throws the specific problem object, executes to throw, the function is over, jumps to the caller, and throws the specific problem object to the caller. That is, if the throw statement is independent, the following do not define other statements because they are not executed.
3. Throws represents a possibility of an exception that does not necessarily occur, throw throws an exception, and execution throw must throw an exception object.
4. Both are negative ways to handle the anomaly (the negative here is not to say that this is bad), just throw or possibly throw an exception, but not by the function to handle the exception, the real handling of the exception by the function of the upper call processing.
2, targeted processing mode: Catch the exception

try{
//Possible exception code
}
 catch (Exception class variable)
{
//Handle exception code, capture
}
finally{
//code that will be executed

iii. The difference between runtimeexception and checkedexception

RuntimeException: When you define a method, you do not need a declaration to throw a runtimeexception, you do not need to capture the runtimeexception when you call this method; or throws mechanism to handle the
Checkedexception: When you define a method, you must declare all the exception that might be thrown, and when you call this method, you must capture its checked exception, Or else it's going to have to pass the exception.
in summary, a method must declare all of the checked exceptions that might be thrown, not check for exceptions that are either uncontrollable (Error), or should be avoided (runtimeexception). If the method does not declare all possible checked exceptions, the compiler gives an error message

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.