Java displays errors in localization mode. When writing support programs, it always needs to display certain error information, programs that support internationalization and localization use the user's local character set to display the corresponding error information based on the information provided by the operating system. This program processes the corresponding error display information based on the corresponding exceptions. It provides the corresponding error message for each exception. Implementation Method: create corresponding error messages for each exception class in the resource file, load these resources in the program, and use Errors as the root for searching in the resource, the class name of the exception object is used as the resource name. Program code compilation:
1. Compile the basic framework of the LocalizedError class, which includes the display () method and main () method. The code is as follows:
The code is as follows: |
Copy code |
Public class LocalizedError { Public static void display (Throwable error ); Public static void main (String [] args ); } |
2. The Display () method is used to Display localized errors. The code is as follows:
The code is as follows: |
Copy code |
Public static void display (Throwable error) { ResourceBundle bundle; // Obtain Required Resources Try { Bundle = ResourceBundle. getBundle ("Errors "); } Catch (MissingResourceException e) { Error. printStackTrace (System. err ); Return; } // Use the error class name as the resource name to query localized messages in the resource // If not, the localization error cannot be displayed. String message = null; Class c = error. getClass (); While (message = null) & (c! = Object. class )) { Try { Message = bundle. getString (c. getName ()); } Catch (MissingResourceException e) { C = c. getSuperclass (); } } If (message = null ){ Error. printStackTrace (System. err ); Return; } String filename = ""; Int linenum = 0; Try { StringWriter sw = new StringWriter (); PrintWriter out = new PrintWriter (sw ); Error. printStackTrace (out ); String trace = sw. toString (); Int pos = trace. indexOf (':'); // search for the first colon If (error. getMessage ()! = Null) // if a localized error message is found Pos = trace. indexOf (':', pos + 1); // search for the second colon Int pos2 = trace. indexOf (')', pos); // find the end of row N Linenum = Integer. parseInt (trace. substring (pos + 1, pos2); // Line N Pos2 = trace. lastIndexOf (', pos); // return to the header of the file name Filename = trace. substring (pos2 + 1, pos); // get the file name. } Catch (Exception e ){;} // Set the parameter table used together with the obtained message String errmsg = error. getMessage (); Object [] args = { (Errmsg! = Null )? Errmsg: ""), error. getClass (). getName (), Filename, new Integer (linenum), new Date () }; // Display error messages System. out. println (MessageFormat. format (message, args )); }
|
3. The Main () method is used to read files. The local display is incorrect:
The code is as follows: |
Copy code |
Public static void main (String [] args) { Try { FileReader in = new FileReader (args [0]); } Catch (Exception e) { LocalizedError. display (e ); } } |
4. The LocalizedError class introduces the following package:
The code is as follows: |
Copy code |
Import java. text .*; Import java. io .*; Import java. util .*; |