IE accident treatment: I have a relationship, I am wrong

xiaoxiao2021-03-06  15

Do you understand the concept of "accident processing"? Accidents are an important feature of advanced programming languages ​​(such as C , Java, and VBScript), which is responsible for controlling what operations should be performed when an application is incorrect. Independent JavaScript does not support accidents, which leads to some programmers prefer to use VBScript instead of JavaScript. But since IE5, this aspect of JavaScript has been supplemented and enhanced. By increasing unexpected processing functions to the grammar, Microsoft enhances JavaScript to C and Java language standards. We have reason to believe that some VBScript programmers will move quickly to JavaScript camp.

Detailed content is:

How to write a JavaScript leading to unexpected accident code

How to write a user leads to unexpected accident code

How to write users in the application leading to unexpected accident code

JavaScript, C and Java accidental processing distinction

Accidental Differences for JavaScript and VBScript

How to avoid contradiction between JavaScript and VBScript

In many programming languages, when trying to execute an illegal instruction, the code will cause a runtime accident. One of the most insignificant illegal operations we may experience is "divided by 0" error. JavaScript does not perform this, it will assign a value to the result variable. Other illegal operations are also common for other programming languages, including JavaScript. Accessing an empty pointer or an empty object is also an example, an array element that accesses an extra-array legal range is another example.

For developers, it is usually necessary to avoid system errors. System information is usually very vague, and the general users don't understand. In fact, system errors will seriously affect a programmer's reputation because they are clear evidence of bugs (bugs). We hope to find these errors in your code, discover them before they attack the operating system to avoid errors. Finding accidents in the code will cause the code to be bloated. When the language does not support accident processing, it is inevitable that the normal code stream and error findings are inevitable. The following code will look for two errors - access an empty object and an array element of the exceeding range:

Var newobject = creteObject (datapiece1, datapiece2, datapiece3);

IF (newObject == null) {

// Do Something Herehen The Object Was Not Created

...

}

Else {

// getElementIndex return an index or -1 for an error

Var index = newobject.getlementIndex ();

IF (INDEX == -1) {

// Handle The Error Case

...

}

Else {

{

// finally, you can do something with index

...

}

The key to supporting accident processing in JavaScript (the same in other languages, this article will explain later) is a try ... catch statement. Try This keyword guides a piece of processed normal processing code, and the catch keyword boots an unexpected processing code. Once in this area, an accident is triggered, and the control of the program process is switched to the first CATCH area followed. But once JavaScript is triggered (this is the programmer opposed), there is no mechanism to determine the type of error. The following is a try ... catch code area used to simulate the above flow: try {

Var newobject = creteObject (datapiece1, datapiece2, datapiece3);

Var index = newobject.getlementIndex ();

// Do Something with INDEX

}

Catch {

Alert ("System Error; Call Your Sw Vendor);

}

Here is another example, you can actually run on your computer:

eXample 1 </ title></p> <p></ HEAD></p> <p><Body></p> <p><Img src = "doc.gif"></p> <p><Script language = "javascript1.2"></p> <p><! -</p> <p>Try {</p> <p>For (image = 0; Image <Document.Images.Length 2; Image ) {</p> <p>Alert (Document.Images [image] .border);</p> <p>}</p> <p>}</p> <p>Catch (kuku) {</p> <p>Alert ("We Have Experienced A JavaScript Error");</p> <p>}</p> <p>// -></p> <p></ Script></p> <p></ Body></p> <p></ Html></p> <p>The accident performed in this example is an array element outside the array boundary. On the page (Doc.gif), the number of graphics is Length (1), so the maximum pointer used is Length 1 (2). Please pay attention to the variable kuku passing to the Catch (Capture) area. JavaScript allows you to deliver any variable types, which will be explained in later pages. Although we don't use this variable, you have to specify one.</p> <p>Accidents that handle programmers</p> <p>Not all accidents are caused by JavaScript, and some are intentionally manufactured by programmers. In fact, a good software engineer puts the use of a large number of unexpected code as a way of simplifying the code. The reason is that the normal processing stream is separated by the normal processing stream, so that the code readability is stronger and easier to maintain and modify. Whenever you want to handle a specific error, you can make an accident. You don't have to use if ... Else to switch between normal streams and unexpected processing streams. Once an unexpected occurred, the flow control switches to the next Catch (Capture) area, where judgment and error processing are performed. Since the programmer fully understands which accident is what he is manufactured, he can design such a mechanism, which is easy to calculate which is unexpectedly and provides useful information to the user.</p> <p>The following simple example shows the accident mechanism you can use:</p> <p><Html></p> <p><HEAD></p> <p><Title> eXample 1 </ title></p> <p></ HEAD> <body></p> <p><Script language = "javascript1.2"></p> <p><! -</p> <p>Function CreateException (msgnum, msgtext) {</p> <p>THIS.MESSAGENUMBER = msgnum;</p> <p>THISGTEXT = MsgText;</p> <p>}</p> <p>Function triggerexception () {</p> <p>ExceptionObj = New CreateException (1, "Error Blablabla");</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>Function raiseexception () {</p> <p>Try {</p> <p>Triggerexception ();</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>Alert ("Call The Programmer ASAP")</p> <p>}</p> <p>Else {</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>}</p> <p>}</p> <p>RaiseException ();</p> <p>// -></p> <p></ Script></p> <p></ Body></p> <p></ Html></p> <p>The main program has only one line of scripts: raiseexception (). As indicated by the function name, the only purpose of this program is to generate an accident. The main part of the raiseException () function contains the Try ... catch statement:</p> <p>Function raiseexception () {</p> <p>Try {</p> <p>Triggerexception ();</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>Alert ("Call The Programmer ASAP")</p> <p>}</p> <p>Else {</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>}</p> <p>}</p> <p>The TRY code area creates an unexpected object and triggers it:</p> <p>Function triggerexception () {</p> <p>ExceptionObj = New CreateException (1, "Error Blablabla");</p> <p>ThrowExceptionObj;</p> <p>}</p> <p>Unexpected object</p> <p>Accidental object is a rule of JavaScript object. At this time, we name the object to ExceptionObj and name it Kuku. The purpose of this accident is to deliver data from the TRY code area to the Catch code area to decipher it and report the details to the user. Whether you want an unexpected object to include how much properties and methods can be included. Two properties are defined in our example: the number of information and its text information. Object Creation Functions are rules JavaScript:</p> <p>Function CreateException (msgnum, msgtext) {</p> <p>THIS.MESSAGENUMBER = msgnum;</p> <p>THISGTEXT = MsgText;</p> <p>}</p> <p>Catch (capture) code area has only one parameter, which can be any data type. Passing to the object of the Catch code area communicates with the number of errors and error information. Note The first object type is first confirmed:</p> <p>EXCEPTIONOBJ InstanceOf CreateException</p> <p>This confirmation is necessary to distinguish between JavaScript and programmers. As its name, the Catch code area captures all the accidents from the related TRY code area. In order to get the user to get the correct error message, we need to determine that the accident is caused by JavaScript (eg, an array element of access to the range) or caused by programmers. The best way is to confirm whether the accident object is the same type with the objects used to create them. If the type matches, we can definitely unexpectedly to deal with it. In the example here, only one information is displayed to the user: if (ExceptionObj InstanceOf CreateException) {</p> <p>Alert ("Call The Programmer ASAP")</p> <p>}</p> <p>If the type does not match, we will continue to create unexpectedly. Accidental treatment is nested. You can place a try ... catch statement in another TRY code area, which is captured by his mother Catch code area whenever a surprise is created from the first CATCH code area. In our example, since there is no higher level Try ... catch statement, it is handled by the operating system. The operating system is responsible for handling accidents that have not been processed by the Catch code area that were previously less low. Below is an unexpected throw in our example:</p> <p>Else {</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>Let's enhance a little script function. In the above script, we specify the number of information and the description when creating an accident object:</p> <p>ExceptionObj = New CreateException (1, "Error Blablabla");</p> <p>But this triggerexception () and raiseexception () functions apply only to an unexpected type. In order to make them more versatile, we increase the number of errors and text parameters to the above two functions:</p> <p><Html></p> <p><HEAD></p> <p><Title> eXample 1 </ title></p> <p></ HEAD></p> <p><Body></p> <p><Script language = "javascript1.2"></p> <p><! -</p> <p>Function CreateException (msgnum, msgtext) {</p> <p>THIS.MESSAGENUMBER = msgnum;</p> <p>THISGTEXT = MsgText;</p> <p>}</p> <p>Function TriggeRexception (Messagenum, Messagetxt) {</p> <p>ExceptionObj = new createException (Messagenum, Messaget);</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>Function raiseexception (num, message) {</p> <p>Try {</p> <p>Triggerexception (NUM, Message);</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>Alert ("Call The Programmer ASAP")</p> <p>}</p> <p>Else {</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>}</p> <p>}</p> <p>RaiseException (1, "DOC JavaScript Column 36);</p> <p>// -></p> <p></ Script></p> <p></ Body></p> <p></ Html></p> <p>Accidents of programmers: a real example (1)</p> <p>Now, we use the unexpected handlers in the previous page to process a more practical example. Suppose there is a <IMG> tag in your page, the program receives some attribute names and extracts the corresponding graphic object attribute value. It may be caused by an array element other than the incorrect attribute and range of access to incorrectness. Here is a complete code:</p> <p><Html></p> <p><HEAD></p> <p><Title> eXample 1 </ title></p> <p></ HEAD></p> <p><Body></p> <p><Img src = "doc.gif"></p> <p><Script language = "javascript1.2"></p> <p><! -</p> <p>Function CreateException (msgnum, msgtext) {</p> <p>THIS.MESSAGENUMBER = msgnum;</p> <p>THISGTEXT = MsgText;</p> <p>}</p> <p>Function TriggeRexception (Messagenum, Messagetxt) {</p> <p>ExceptionObj = new createException (Messagenum, Messaget);</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>Function raiseexception (num, message) {</p> <p>Try {</p> <p>Triggerexception (NUM, Message);</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>Alert ("Call The Programmer ASAP")</p> <p>}</p> <p>Else {</p> <p>Throw ExceptionObj;</p> <p>}</p> <p>}</p> <p>}</p> <p>FieldNames = New Array ("Border", "Complete", "Height", "HSpace", "LowSRC", "Name", "SRC", "VSPACE", "Width");</p> <p>Try {</p> <p>Image = 0; Image <Document.Images.Length 1; Image ) {</p> <p>For (i = 0; I <fieldnames.length; i ) {</p> <p>IF (Document.Images [Image] == NULL) Triggerexception (98, "Illegal Access To iMages Array);</p> <p>IF ("document.images [image]." fieldnames [i])! = null) Alert (FieldNames [i] "IS" EVAL ("Document.images [Image]." FieldNames [i] ))</p> <p>Else Triggerexception (I, "Field Name" FieldNames [i] "is not legal");</p> <p>}</p> <p>}</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>if (exceptionObj.messageNumber> 0 && exceptionObj.messageNumber <9) alert ( "Problem in property:" exceptionObj.messageText) else if (exceptionObj.messageNumber <= 98 && exceptionObj.messageNumber> = 97) alert ( "Problem in images Array: " ExceptionObj.Messagetext)</p> <p>Else RaiseException (99, "catch all") ;;</p> <p>}</p> <p>}</p> <p>// raiseException (1, "DOC JavaScript Column 36);</p> <p>// -></p> <p></ Script></p> <p></ Body></p> <p></ Html></p> <p>Accidents of programmers: a real example (2)</p> <p>First we define the array fieldnames. The TRY code area includes two nested loops, and the outside is looped between the graphics of the page:</p> <p>Image = 0; Image <Document.Images.Length 1; Image ) {</p> <p>The loop inside is cycled between the fieldNames elements:</p> <p>For (i = 0; I <fieldnames.length; i ) {</p> <p>We check 2 accidents inside the circulation. The first test has an attempt to access an array element in the range of access. When there is an invalid attempt, we started an accident:</p> <p>IF (Document.Images [Image] == NULL) Triggerexception (98, "Illegal Access To iMages Array);</p> <p>The second verification of print-related object properties or when the domain name does not match any object's domain name:</p> <p>IF ("document.images [image]." fieldnames [i])! = null) Alert (FieldNames [i] "IS" EVAL ("Document.images [Image]." FieldNames [i] ))</p> <p>Else Triggerexception (I, "Field Name" FieldNames [i] "is not legal");</p> <p>Catch (capture) code area decrypts unexpected objects. First we verify that the object is the correct type:</p> <p>EXCEPTIONOBJ InstanceOf CreateException {</p> <p>As mentioned earlier, this test is critical to distinguishing JavaScript errors and user errors. Once we know that it is an accident caused by a user, we will test whether the number of errors is between 1-9, and print an appropriate information:</p> <p>IF (ExceptionObj.Messagenumber> 0 && ExceptionObj.Messagenumber <9) Alert ("Problem in property:" exceptionobj.MessageText)</p> <p>We check if the number of errors is 97 or 98, and print an unexpected information:</p> <p>Else IF (ExceptionObj.Messagenumber <= 98 && excseptionobj.Messagenumber> = 97) ALERT ("Problem in images Array: ExceptionObj.MessageText) If the unexpected object does not match any error type, we trigger a use of operating system processing Ordinary accident:</p> <p>Else RaiseException (99, "catch all")</p> <p>Be sure, if you don't deal with this accident, others will deal with it. The operating system handles unexpected unwanted people.</p> <p>Comparison with Java, C </p> <p>Please don't surprise, you will find that Java and C accident syndrome is very similar to JavaScript. As in JavaScript, they are also centered on try .catch statements. The main difference is the variable of Catch. As explained in the previous page, this variable in JavaScript can be any data type, which can be an object, a string, an integer, or any other supported type. In fact, JavaScript does not support type declarations, so it is unable to declare the type of variables of CATCH. In Java, Catch's variable is an exemplary of the Java class, which extends standard Java into categories. The Catch statement should be like this:</p> <p>Catch (ObjectType Exception)</p> <p>Among them, it is an object of an ObjectType type. C is not so limited, CATCH variables can be an exemplary example of standard C accidents, or any C type:</p> <p>Catch (char * message)</p> <p>Because there is no type declaration in JavaScript, it only supports a Catch code area in each TRY code area. In the previous page, we have shown that when our accidents are dependent on the variable type, we use the instanceof function to check if it is the correct type. In C and Java, you can use multiple Catch code districts for each TRY code area. Each Catch code area is responsible for processing an appropriate type. If you want to deal with the CHAR type and myException type:</p> <p>Try {</p> <p>// FOLLOWING FUNCTION Can Throw Exceptions of Type Char orexception</p> <p>DOSMETHINGUSEFUL ();</p> <p>}</p> <p>Catch (char * eXceptionstring) {</p> <p>// Handle The Case That Exception Is A String</p> <p>}</p> <p>Catch (MyException ExceptionObject)</p> <p>// Handle The Case That Exception Is An Instance of Type MyException</p> <p>}</p> <p>In JavaScript, you have to use these code:</p> <p>Try {</p> <p>// FOLLOWING FUNCTION Can Throw Exceptions of Type Char orexception</p> <p>DOSMETHINGUSEFUL ();</p> <p>}</p> <p>Catch (exception) {</p> <p>// Verify Exception is of myExceptionObject Type</p> <p>IF (Exception InstanceOf myExceptionObject) {</p> <p>IF (Exception.kind == "String") {// Handle The Sting Exception</p> <p>}</p> <p>Else IF (Exception.kind == "MyException") {</p> <p>// Handle The Object Exception</p> <p>}</p> <p>}</p> <p>Another difference is Java and C support unexpected Catch-ALL (full capture), which is to capture all accidents in a catch code area. The syntax in Java is:</p> <p>Catch (throwable t) {</p> <p>// Handle All Cases</p> <p>}</p> <p>Where throwable is a prior defined Java class. The corresponding catch-all syntax in C is:</p> <p>Catch (...) {</p> <p>// Handle All Cases</p> <p>}</p> <p>As mentioned above, since JavaScript only supports a Catch code area, all accidents are captured by the same catch code area according to the definition. You should use:</p> <p>Catch (exception) {</p> <p>// Handle All Exceptions</p> <p>}</p> <p>The same difference is also present in C , Java, and JavaScript. The syntax is the same in three languages:</p> <p>Throw ExceptionObject;</p> <p>Since Java can only capture the accident of the throwable type, ExceptionObject must be this type. C and JavaScript can throw any data types, including strings, integers, objects, etc.</p> <p>C and Java are also different from JavaScript in the way in which Java is developed. If an accident is not handled in C or Java, then in the call scheme, it is automatically upgraded to a higher level, such as call functions, main programs, or browsers. In JavaScript, if you want a higher level to handle this accident, you have to come to improve this accident. I have already shown in the previous page, you should use this simple syntax:</p> <p>Throw ExceptionObj;</p> <p>The last difference is that C and Java do not support the nested of the TRY..CATCH code area. In JavaScript, you can put a try ... catch statement in the TRY or CATCH area of ​​another statement.</p> <p>Compared with VBScript</p> <p>VBScript supports unexpected support than JavaScript's new accident. If you still have to use VBScript because you like your error handling, you have no sufficient reasons, because JavaScript can now handle unexpected. The principle of VBScript unexpected processing is simple, and the function is called and then the error is checked.</p> <p>JavaScript is to capture accidents with a catch statement, while VBScript is captured with an ON Error statement. Omitting the ON Error statement will spread all unexpectedly to your browser, and the browser displays the user an error dialog. In fact, JavaScript also has this behavior. If you don't use the try ... catch statement, all accidents are handled by the browser and display an error message to the user. As mentioned earlier, the user is not very clear about where he should go, so you must have an unexpectedly before the browser. If considering the accident, such a Web site belongs to a higher level.</p> <p>The ON Error statement is very intuitive:</p> <p>ON Error ResMe next</p> <p>This statement is only open. If there is an accident in any of the next row, running from the row of rows that follow the row, the error corrects you call the row that caused an unexpected function. In JavaScript, it is quite trory ... catch statement: try {</p> <p>// Exceptions in this Block Will Be Handled by The Following Catch Block.</p> <p>// any Exception Will Quit this block and switch control to the catch block</p> <p>}</p> <p>Catch (ExceptionObj) {</p> <p>// Handle Exception from The Try Block ABOVE</p> <p>// code in this block of be el executed before the line immediately</p> <p>// Following the Culprit Line That Caused The Exception</p> <p>}</p> <p>The main difference captures unexpected in VBScript and JavaScript is: Once you have opened the error handling, you can't turn it off in VBScript. In VBScript, error handling can only be shut down at the end of the function. In JavaScript, you can include the statement area you want to open in the TRY code area. Simply put, in VBScript and JavaScript, you can control the beginning of a zone. And only in JavaScript can control the end of a zone. In VBScript, the end of the area is the end of the function.</p> <p>Handling unexpectedly in VBScript is much more complicated than capturing them. After each suspicious statement, you have to check the error:</p> <p>`Turn on Error Handling</p> <p>ON Error ResMe next</p> <p>`Clear the Err Object</p> <p>Err.clear</p> <p>`The Following Statement or Procedure Call Might Cause An Exception</p> <p>Kuku ()</p> <p>`Check if an exception was raised</p> <p>IF err.number> 0 THEN</p> <p>`Handle The Exception Here</p> <p>END IF</p> <p>You have noticed the ERR object. Accidental processing in VBScript is based on it. It has six attributes that are previously set: Number, Description, Source, HelpFile, HelpContext, Lastdllerror (last DLL error). JavaScript's Error object shared two of these properties</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-46956.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="46956" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.050</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'k1OQDRytfE0zdHrYcG95DgZg1B2u9agVmJVTCMeZlVjWqeYVP3d40RrA1ZAPVENG8PUpb4GT_2Bnsm1fSb'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>