Research on the exception mechanism in Java

Source: Internet
Author: User

First, Java exceptions are object-oriented. A Java exception is an object that describes an exception. When an exception occurs, an exception object is generated and stored in the abnormal member function.

Java exception handling is implemented through five keywords: Try, catch, throw, throws, and finally.
1. Exception Handling:
The error handling structure in Java consists of three parts: Try, catch, and finally. The try block stores the Java language where exceptions may occur and manages relevant exception pointers. The Catch Block follows the try block to stimulate caught exceptions; finally blocks include resources and handles not released by the purge program. The finally block is executed no matter how the code in the try block exits.
2. Throw an exception:
The Java language can not be directly captured in the method, but throw the exception to the upper-layer caller. The throw statement is used to explicitly throw an exception. First, you must obtain a throwable instance handle, pass the parameter to the catch, or use the new operator to create an exception.
Example: Public void execute (string STR, int index) throws whcaexception {
Try {
// Code that may generate exceptions;
} Catch (exception e ){
Throw new whcaexception ("JB: M:" + E. getmessage );
}
}
Finally, let's talk about the notes for using the "exception mechanism ".

1. Multiple exceptions may occur in a method. You can set multiple "exception" throws to solve this problem.

2. an "exception" object is a value transfer process from the time it is generated to the time it expires after it is captured. Therefore, you can, to control the granularity of "exceptions.

3. "exception mechanism" also has a special situation-runtimeexception "exception class", which has a feature for both the "exception class" and all its subclasses, when an "exception" object is generated, it is directly processed by the Java Virtual Machine. That is, the throw clause in the method is easily captured by the virtual machine. Therefore, when a method that throws this "RunTime exception" is referenced, try… is not required... Catch statement to handle "exceptions ".
4. if an exception is caught, appropriate processing is required. Calling printstacktrace is helpful for debugging programs, but there is no real exception!

The printstacktrace () method can only analyze the program running status. The stacktraceelement class is provided in jdk1.4 to obtain the program running status! Example:

Import javax. Swing .*;

Public class stacktracetest
{
Public static int factorial (int n ){
System. Out. Print ("iteration: (" + N + "):");
Throwable T = new throwable ();
Stacktraceelement [] frames = T. getstacktrace ();
For (INT I = 0; I <frames. length; I ++)
System. Out. println (frames [I]);
Int R;
If (n <= 1) r = 1;
Else r = N * factorial (n-1 );
System. Out. println ("return" + r );
Return R;
}
Public static void main (string [] ARGs ){
String input = joptionpane. showinputdialog ("Enter N :");
Int n = integer. parseint (input );
Factorial (N );
System. Exit (0 );
}
}


 

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.