Throw 语句

简介: Throw 语句

Throw 语句
throw 语句允许我们创建自定义错误。

正确的技术术语是:创建或抛出异常(exception)。

如果把 throw 与 try 和 catch 一起使用,那么您能够控制程序流,并生成自定义的错误消息。

语法
throw exception
异常可以是 JavaScript 字符串、数字、逻辑值或对象。

实例
本例检测输入变量的值。如果值是错误的,会抛出一个异常(错误)。catch 会捕捉到这个错误,并显示一段自定义的错误消息:

My First JavaScript

Please input a number between 5 and 10:


目录
相关文章
|
11月前
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
每日一道面试题之try-catch-finally 中,如果 catch 中 return 了,finally 还会执行吗?
144 0
|
4月前
|
编译器
try{...}catch(){...}finally{...}语句你真的理解吗?
try{...}catch(){...}finally{...}语句你真的理解吗?
32 0
|
4月前
|
C++
C++异常处理try和throw以及catch的使用
C++异常处理try和throw以及catch的使用
try catch finally,try 里面有 return,finally 还执行吗?
try catch finally,try 里面有 return,finally 还执行吗?
50 0
|
4月前
|
存储 缓存 IDE
细琢磨,try catch finally 执行顺序与返回值
细琢磨,try catch finally 执行顺序与返回值
52 0
try...catch中,catch加了return,后面的代码是不会执行的
try...catch中,catch加了return,后面的代码是不会执行的
96 0
|
存储 IDE Java
try catch finally 执行顺序总结
try catch finally 执行顺序总结
114 0
throw 语句
throw 语句
48 0
try catch return语句情况分析
简要讲述一下try catch代码中return语句返回值情况分析
|
JavaScript 前端开发
错误与异常 之 try...catch语句
错误与异常 之 try...catch语句
111 0