Java Exception Handling Summary

Source: Internet
Author: User

An exception is an object of type exception.

1 Exception and Error

Exception and error inherit from the Throwable class, the difference between the two:

Exception: Generally represents a problem in the program, you can directly use the Try...catch processing, the class of Printstacktrace () method output exception information is more complete.

Error: General refers to JVM errors, which cannot be handled in the program.

All exceptions in Java that have a small capture range must be placed before an exception that captures a large range (otherwise the compilation will error). For a program, if there are multiple exceptions, it is best to capture them separately rather than using exception to catch all exceptions directly.

2 Throws and throw

Throws: When defining a method, you can declare it using throws, and the method declared using throws means that the method does not handle the exception and is passed to the call of the method for processing.

Throw is used to artificially throw an exception, and when thrown, directly throws an instantiated object of the exception class.

Throwdemo.java

The class math{public int div (int i,int j) throws exception{//defines the division operation, and if there is an exception, it is transferred to the manipulated System.out.println ("* * * * * * * * * * * * * * * *)"; int temp = 0;//define local variable try{temp = i/j;//calculation, but there may be an exception}catch (Exception e) {throw e;} finally{//the Uniform Export System.out.println ("* * * * * * * * * * * * * * * *), whether or not it is abnormal;} return temp;}; public class Throwdemo{public static void Main (String args[]) {Math m = new Math (); Try{system.out.println ("Division operation:" + M.Div ( 10,0));} catch (Exception e) {System.out.println ("exception generated:" + E);}};

Note: Throw can be used in conjunction with a try, catch, or throws statement, but cannot exist independently; catch and finally cannot have no program between Try;try and catch.

3 Exception and RuntimeException

Inheritance relationship: Exception->runtimeexcetion->illegalargumentexcetion->numberformatexcetion; Exception's throw is essentially a polymorphic application.

In general, exception must be handled with Try...catch in the program, but runtimeexception can be processed without try...catch, and if an exception is generated, the exception will be handled by the JVM.

Even if it is not an runtimeexception exception, you can use the throws keyword to avoid exception handling, which is essentially duck way. But ducking is only in buck, sooner or later someone will have to deal with this matter. If main () also duck out the exception, eventually only the JVM will handle the exception, and the Java Virtual Machine is not responsible for the exception, which will eventually cause the program to break execution. Therefore, it is better to be an upright programmer.

Java Exception Handling Summary

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.