try...catch...finally的理解
以前,我一直喜欢用console.log(do some thing)去执行输出的类型和值,想马上看到弹出的信息,就会直接在浏览器alert()一下,这些是基础知识。
稍微复杂一点点,就要用到判断语句,if else进行条件判断,话说if条件else否则,这样的判断对于写程序代码的码侬已经是非常熟悉不过了。
如果你觉得这个也很简单,可能会用到混合if else条件判断语句加上try catch 来处理语句,虽然用try catch能处理任何的对象,通过throw扔一条有错误的语句,接着catch抛出该对象或者该对象的错误,今天我们只说try...catch,下面的例子分别抛出数组、时间、原型函数、数字类型等。
function trycatch () {
var array = [234],
newdate = new Date(),
fun = function(){},
is = 12.22,
call;
try{
throw array + '\n' + newdate.toLocaleString() + ' \n' + fun.prototype.constructor +
'\n' + (typeof is == 'number') +' \n' + call ; //小心local后面还有一个'e'
}
catch(e){
console.log(e);
}
finally{
console.log('err finally');
}
}
trycatch ()
// 输出:
// 234
// 2015/10/12 下午10:07:03
// function (){}
// true
// undefined 更准确的说,try内放一条可能产生错误的语句。当try语句开始执行并抛出错误时,catch才执行内部的语句和对应的try内的错误信息message。何时执行finally语句,只有当try语句和catch语句执行之后,才执行finally语句,不论try抛出异常或者catch捕获都会执行finally语句。
function trycatch () {
try{
throw new Error('koringz');
}
catch(e){
console.log(e.message);
}
finally{
console.log('err finally');
}
}
trycatch ()
// 输出:
// koringz
// err finally 通过try扔出一条错误的语句,我们看到在catch捕获到一条错误的的信息// koringz,但是同样的finally也输出了// err finally。虽然我们了解try catch工作流的处理方式,但是并不了解finally块的代码处理程序,按照以往我们对finally语句一贯的思维方式,就是finally输出不受try和catch的限制和约束。以下是finally的几个输出演示代码:
function trycatch () {
try{
throw new Error('koringz');
}
finally{
console.log('err finally');
return console.log('new finally')
}
}
trycatch ()
// err finally
// new finally 如上所示,try扔一条错误的语句,finally输出的结果是: // err finally // new finally。
function trycatch () {
try{
throw new Error('koringz');
}
catch(e){
console.log('err finally');
return console.log('new finally')
}
}
trycatch ()
// err finally
// new finally
如上所示,try扔一条错误的语句,catch捕获到错误输出结果同上finally。 // err finally // new finally。
当我修改try的语句:
function trycatch () {
try{
//
}
catch(e){
console.log('err finally');
return console.log('new finally')
}
}
trycatch ()
// 空(viod)
// 空(viod) 结果就输出都为空。// 空(viod)。因为try没有扔出错误,所以catch没有捕获到异常,故输出结果就为空。
那么我们再看看下面这个案例,通过下面的例子,可能会让你更加地了解try catch语句的异常处理。
try{
try{
throw new Error('open');
}
catch(e){
console.info(e.message);
throw e
}
finally{
console.log('finally');
}
}
catch(e){
console.log('op',e.message);
}
// open
// finally
// op open 当我们在try可能引发错误的代码块内嵌套try catch,通过嵌套的代码块try内扔一条可能出现错误的语句 throw new Error('open');,紧接着嵌套的try将错误传递给嵌套的catch处理,最终通过嵌套的finally运行过后,我们看到最后一条结果// op open,其实嵌套的catch捕获的错误信息扔给最外层catch捕获的。// op open
也就是说:任何给定的异常只会被离它最近的封闭catch块捕获一次。
当然,在“内部”块抛出的任何新异常(因为catch块里的代码也可以抛出异常),都将会被“外部”块所捕获。
javascript中try catch finally 的使用
try...catch...finally 语句 为 JScript 实现错误处理。 try { tryStatements} catch(exception){ catchStatements} finally { finallyStatements} ============= 参数 tryStatement 必选项。可能发生错误的语句。 exception 必选项。任何变量名。exception 的初始化值是扔出的错误的值。 catchStatement 可选项。处理在相关联的 tryStatement 中发生的错误的语句。 finallyStatements 可选项。在所有其他过程发生之后无条件执行的语句。 说明 try...catch...finally 语句提供了一种方法来处理可能发生在给定代码块中的某些或全部错误,同时仍保持代码的运行。如果发生了程序员没有处理的错误,JScript 只给用户提供它的普通错误信息,就好象没有错误处理一样。 tryStatements 参数包含可能发生错误的代码,而 catchStatement 则包含处理任何发生了的错误的代码。如果在 tryStatements 中发生了一个错误,则程序控制被传给 catchStatements 来处理。exception 的初始化值是发生在 tryStatements 中的错误的值。如果错误不发生,则不执行 catchStatements。 如果在与发生错误的 tryStatements 相关联的 catchStatements 中不能处理该错误,则使用 throw 语句来传播、或重新扔出这个错误给更高级的错误处理程序。 在执行完 tryStatements 中的语句,并在 catchStatements 的所有错误处理发生之后,可无条件执行 finallyStatements 中的语句。 请注意,即使在 try 或 catch 块中返回一个语句,或在 catch 块重新扔出一个错误,仍然会执行 finallyStatements 编码。一般将确保 finallyStatments 的运行,除非存在未处理的错误。(例如,在 catch 块中发生运行时错误。)。
示例
下面的例子阐明了JScript 特例处理是如何进行的。
try {
print("Outer try running..");
try {
print("Nested try running...");
throw "an error";
}
catch(e) {
print("Nested catch caught " + e);
throw e + " re-thrown";
}
finally {
print("Nested finally is running...");
}
}
catch(e) {
print("Outer catch caught " + e);
}
finally {
print("Outer finally running");
}
// Windows Script Host 作出该修改从而得出 WScript.Echo(s)
function print(s){
document.write(s);
} 将得出以下结果: Outer try running.. Nested try running... Nested catch caught an error Nested finally is running... Outer catch caught an error re-thrown Outer finally running
如下是Javascript的例外处理的一个实例。
var array = null;
try {
document.write(array[0]);
} catch(err) {
document.writeln("Error name: " + err.name + "");
document.writeln("Error message: " + err.message);
}
finally{
alert("object is null");
} 程序执行过程
1. array[0]的时候由于没有创建array数组,array是个空对象,程序中调用array[0]就会产生object is null的异常 2. catch(err)语句捕获到这个异常通过err.name打印了错误类型,err.message打印了错误的详细信息. 3. finally类似于java的finally,无论有无异常都会执行.
现总结Error.name的六种值对应的信息: 1. EvalError:eval()的使用与定义不一致 2. RangeError:数值越界 3. ReferenceError:非法或不能识别的引用数值 4. SyntaxError:发生语法解析错误 5. TypeError:操作数类型错误 6. URIError:URI处理函数使用不当
|