7

JavaScript 中 try, catch, throw 的用法

 3 years ago
source link: https://knightyun.github.io/2019/09/02/js-try
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

程序在运行中难免遇到 bug,所以就需要好的调试手段找出问题所在,try, catch, throw 便是 JavaScript 中用来调试并对错误执行相关操作的工具,下面具体介绍其用法;

try, catch

基本语法结构:

try {
    // ...
    // 这里写需要调试的代码段
} catch(error) {
    // ...
    // 这里写要对获取的错误信息执行的操作
}
try {
    // 这里故意写错函数名,为了抛出错误
    console.logg('This is an error and will not display');
} catch (e) {
    console.log(e);         // TypeError: console.logg is not a function
    console.log(e.message); // console.logg is not a function
    console.log(e.name);    // TypeError
    console.log(e.stack);   // TypeError: console.logg is not a function
}

上面的错误代码如果直接在正常环境中执行,便会直接在后台输出错误:

TypeError: console.loggg is not a function

但是使用 try, catch 结构的话,就可以获取一个包含错误信息的对象,其包含各个部分的错误信息,便于进行一些自定义操作;

throw

throw 是在上述结构中使用的一个函数,接受一个参数作为输出信息,throw 的作用是中断后面所有语句的执行,包括错误源,但是它前面的语句都会正常执行,它可以用于判断错误的具体位置,例如:

try {
    console.log('This will display.');
    throw('My error position.'); // throw 将会中断语句的执行
    // 同样故意制造错误
    console.logg('This is an error and will not display.');
    // 后面是正常语句
    console.log('This will not display, either.')
} catch (e) {
    console.log(e);
}
// This will display.
// My error position.

如果错误发生在 throw 语句之前的话,错误便会被正常抛出,而 throw 传递的信息不会被输出,例如:

try {
    console.logg('This is an error and wil not display.');
    throw('My error position.');
    // 后面的执行同样会被中断
    console.log('This will not display, either.')
} catch(e) {
    console.log(e); 
}
// TypeError: console.logg is not a function.

因此,在调试过程中可以结合上面两种情况,一步步找出错误的具体位置;


Recommend

  • 114
    • Imgur imgur.com 7 years ago
    • Cache

    Try catch ftw. - Imgur

    Try catch ftw. Add to FavoritesShareTry catch ftw.

  • 40
    • 掘金 juejin.im 6 years ago
    • Cache

    驳《慎用 try catch》

    今天在掘金看到了一篇文章,慎用 try catch,发布者的昵称是“前端妹子”。根据我的经验,这种昵称一般都不是妹子,大概率是营销号(PS:如果能换个美女头像就更走心了)。(这个还真是个妹子,www.facebook.com/raoenhui 之前言论欠妥,在

  • 40
    • 掘金 juejin.im 6 years ago
    • Cache

    慎用try catch

    前言 ECMA-262第3版引入了try catch语句,作为JavaScript中处理异常的一种标准方式。基本的语法如下所示。 但是在前端js代码中很少看到try catch语句,并不是所以代码都需要加try catch来作得不偿失的“保险”,下面来分析作为

  • 28
    • www.v2ex.com 5 years ago
    • Cache

    为什么不建议用 try catch?

    Java - @wysnylc - 不问是不是,就问为什么。这个问题看来需要从头说起。一句话解释:try catch 机制非常好。那些觉得 try catch 不行的人,是他们自己的水平有问题,无法理解这种机制。并且这群人写

  • 26
    • www.androidchina.net 4 years ago
    • Cache

    OutOfMemoryError 可以被 try catch 吗?

    目录 OutOfMemoryError 可以被 try catch 吗? 捕获 OutOfMemoryError 有什么意义? JVM 中哪一块内存不会发生 OOM ? OutOfMemoryError 可以被 try catch 吗? 群里小伙伴碰到的一道比较经典...

  • 12
    • vector-of-bool.github.io 4 years ago
    • Cache

    Levelling the try/catch Mountain

    Level - verb (used with object). levelled, levelling To make even or flat Brace yourself. This post ends with an informal language...

  • 7

    Interesting throw/catch behaviour in Ruby When I was working on integrating Rodauth with OmniAuth authentication, I noticed an error warning after upgrading to Rails 6.1, w...

  • 4

    Posted on November 30, 2021Novembe...

  • 4
    • meticulous.ai 2 years ago
    • Cache

    JavaScript Try Catch Guide

    JavaScript Try-Catch: A Complete Guide published on 05 May 2022 In any software development project, handling errors and exceptions is vital to the success of your applicati...

  • 11

    Blog / JavaScript / Error Handling in JavaScript: A Guide to Try/Catch, Throw, and Custom Error Classes Erro...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK