Describe the simple principle and application of the exception handling mechanism in Java and the difference between error and exception?

Source: Internet
Author: User

1: Describe the simple principle and application of the exception handling mechanism in Java, and describe the difference between error and exception?

If noProgramIf an exception is generated, the JVM will throw an instantiated object of the exception class, if the try statement is used for capture, exception handling can be performed. If not, it is handed over to the JVM for processing. After the try statement captures an exception, will match the exception type in catch. If the match is successful, use this catch statement for processing.

Application: a simple application is to add try... catch to all throws keywords.
If you follow a standard practice, the try, catch, finally, throw, and thorws keywords should be used together.

When a Java program violates the Java Semantic Rules, the Java Virtual Machine will indicate an error as an exception. There are two types of violation of Semantic Rules. One is the built-in semantic check of the Java class library. For example, if the array subscript is out of the range, indexoutofboundsexception is triggered. When a null object is accessed, nullpointerexception is thrown. Another scenario is that Java allows programmers to extend this semantic check. programmers can create their own exceptions and choose when to use the throw keyword to cause exceptions. All exceptions are subclasses of Java. Lang. thowable.
Eg: First Type
Class test {
Public int devide (int x, int y) throws exception {//
Int result = x/y;
Return result;
}
}
Class testexception {
Public static void main (string [] ARGs ){
Try {
New Test (). devide (3, 0 );
} Catch (exception e ){
System. Out. println (E. getmessage (); // although exception has no other functions except constructor,
// But it inherits the throwable class.
}
System. Out. println ("program running here ");
}
}
If you do not know whether to throw an exception when writing test class, you can add throws exception after the devide Methods method. In testexception, you must handle the thrown exception.
Eg: type 2
If you define an exception class, you must inherit the exception, that is, its subclass.
Class test {
Public int devide (int x, int y) throws exception {//
If (Y <0)
Throw devidemyminusexception ("devide is" + y );
Int result = x/y;
Return result;
}
}
Class devidemyminusexception extends exception {
Public devidemyminusexception (string MSG ){
Super (MSG); // call the construtor of the parent class (exception). The main function getmessage () Outputs a custom exception: "devide is ..."
}
}
Example 3
Multiple exception classes can be thrown.
Class test {
Public int devide (int x, int y) throws arithmeticexception, devidemyminusexception {//
If (Y <0)
Throw new devidemyminusexception ("devide is" + y );
Int result = x/y;
Return result;
}
}
Class devidemyminusexception extends exception {
Public devidemyminusexception (string MSG ){
Super (MSG );
}
}

Class testexception {
Public static void main (string [] ARGs ){
Try {
New Test (). devide (3,-1 );
} Catch (arithmeticexception e ){
System. Out. println ("program running into arithmeticexception ");
E. printstacktrace ();
} Catch (devidemyminusexception e ){
System. Out. println ("program running into devidemyminusexception ");
E. printstacktrace ();
} Catch (exception e ){
System. Out. println (E. getmessage)
) Finally {
System. Out. println ("finally running"); // no matter the program has an exception, finallyCode All blocks must be executed.
}

System. Out. println ("program running here ");
}
}
1. the program matches the exception type. automatically enters the corresponding catch statement. exception should be placed after other exception statements, because they all inherit exception, and other exceptions should be placed behind it, which makes no sense.
2. If there is a return statement in try {}, will the code in finally {} following this try be executed? When will it be executed, before or after return? Will be executed and executed before return.
3. When will the finally code not be executed? When system. Exit (0); is displayed, It is not executed and the program exits.

 

 

Error and exception:

Errors and exceptions in Java come from the throwable class, which is his subclass,

Error: it is an error in the system. The programmer cannot change it and handle it. The error occurs during program compilation. The error can be corrected only by modifying the program.

Exception: an error is caught while the program is running. It is an exception that can be handled.

Syntax: Try {}

Catch (Exception name ){}

Finally

{Clean up resources: for example, Close opened files: delete temporary files ;....}

Exception: it can also be divided into two types: runtimeexception (which is a common error that can not be caught) and other exceptions (which must be caught so that the program can continue, like throw ioexception in a method ).

 

 

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.