Small issues are also entangled: ASP. NET pop-up displays ex. Message exception information

Source: Internet
Author: User

Recently, the Library maintenance system has encountered a small problem: the thrown exceptions cannot be displayed.

Because the user does not have high requirements, it is inappropriate to directly display the exception information to the user without handling the exception. However, it is time-consuming to continue learning, it cannot be too detailed. But it took two hours to solve this small problem! I found a lot of information on the Internet, and basically there is no right. It can be said that no one can give a clear answer.

The process is very simple: use ASP. NET's response. Write method to return a javascript code to the client.CodeThe error message is displayed. The Code is as follows:

 
Try {//....} Catch (exception ex) {response. write ("<script language = 'javascript '> alert (\" "+ ex. message + "\"); </SCRIPT> ");}

 

This is a simple task, but the exception message ex. Message is not displayed, and the interface is just refreshed. After several steps, we finally solved the problem.

1. Replace ex. Message with any string to check that the response. Write method can be used in the catch statement block to display the dialog box. The result is displayed successfully, indicating that the problem lies in ex. Message.

2. InProgramYou can see the content of ex. Message as follows:


The focus is on the red circle. Here there is a carriage return + line break symbol, which is probably the ghost he is engaged in. If you don't say anything, remove it directly. The Code is as follows:

 

 
Try {//....} Catch (exception ex) {response. write ("<script language = 'javascript '> alert (\" "+ ex. message. replace ("\ r \ n", "") + "\"); </SCRIPT> ");}

 

But this still doesn't work! Still not displayed!

3. After a close-to-crash exploration, I finally found out the problem. It is true that \ r \ n is a ghost, but the replacement method is incorrect. \ R \ n () is displayed at the next breakpoint. At this time, \ is an escape character, which is used to escape R and N. The actual meaning is carriage return + line feed. I wrote \ r \ n, which is followed by the first \ escape character. R and n are treated as common characters. The true meaning is \ r \ n. Of course, this cannot be replaced! The correct statement is as follows:

 
Try {//....} Catch (exception ex) {response. write ("<script language = 'javascript '> alert (\" "+ ex. message. replace ("\ r \ n", "") + "\"); </SCRIPT> ");}

In this way, it can be perfectly displayed!

In fact, there are many such questions on the Internet. The answer is wrong because they make the same mistake as me, and many escape characters are written, if you encounter a similar problem in the future, just remove the carriage return and line feed (for example, display a piece of read text, which contains the carriage return and line feed )! I hope this small experience will help you!

 

Related Article

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.