HTML DOMException code property

The code property of the DOMException interface returns a short number that contains the error code of the exception error, or 0 if none match. This is a read-only property.

Syntax:

var codeNumber = domException.code;

Return Value: This property returns a short number.

Example: In this example, we will create an INVALID_CHARACTER_ERR and then show its code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>DOMException code example</title>
</head>
<body>
  <h1>GeeksforGeeks</h1>
  <p>Click below to get the exception code</p>
  <button onclick="get()">Click</button>
  <p class="p">Error Code is : </p>
</body>
<script>
    function get(){
        try {
                // INVALID_CHARACTER_ERR
            var elem = document.createAttribute ("123");
        }
        catch (e) {
            console.log("Error is :", e);
            console.log("Errorcode is :", e.code);
            document.querySelector(
      ".p").textContent+=e.code;
        }   
        
</script>
</html>

chevron_right


Output:



Before Button Click:

After Button Click:

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.