An error occurred while displaying the localization method in Java.

Source: Internet
Author: User
Tags character set getmessage throwable

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 .*;

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.