简体   繁体   中英

How can I filter an error message in a user friendly way without creating a custom string in Android?

I recently created an AlertDialog to inform users that and error has occurred. In the catch block I set the exception message as follows:

try {
// Do stuff
} catch (Exception e) {
        mAlertDialog.setMessage(e.getMessage());
}

This works perfectly fine and the message I see in my AlertDialog is as follows:

Read error: ssl=0xb832f3f0: I/O error during system call, Connection timed out

Is there a way to remove the text so that the error reads as follows:

I/O error during system call, Connection timed out

I know you can use

e.getMessage().substring()
e.getMessage().startsWith()

and what not, but I would really like it to report any type of error that might occur, in a user friendly formate. Maybe using regex? Which I don't have much experience with. I would really appreciate a push in the right direction. All tips, hints, incites and help are greatly appreciated.

Thanks in advance!

You may use replaceFirst .

mAlertDialog.setMessage(e.getMessage().replaceFirst(".*:\\s*", ""));

This would match and replace all the characters upto the last colon with empty string.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM