Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
throw
statement is used to throw exception.
throw RuntimeException("Error Message");
throw
statement can have any expression but generally it should be an exception object.
In general, RuntimeException will be useful for any runtime errors.
As for exception objects, see try-catch-finally for details.
Only when there is throw
in catch clause, no expression do not have to be written
because throw
statement can rethrow a caught exception.
try {
throw RuntimeException("Some error occurred");
} catch (e) {
throw; // e will be re-thrown.
} finally {
/* ... */;
}
throw
statement can have if-modifier
.
See the example below.
throw RuntimeException("Some error occurred") if (a < 10); // throw an exception if `a` is less than 10.
function test(a) {
throw RuntimeException("Some error occurred") if (a < 10);
System.println("No throw exception because a is %{a} which means %{a} >= 10");
}
try {
test(11);
test(10);
test(9);
} catch (e) {
System.println("%{e.type()}: %{e.what()}");
}
No throw exception because a is 11 which means 11 >= 10
No throw exception because a is 10 which means 10 >= 10
RuntimeException: Some error occurred
try {
throw; // what is thrown?
} catch (e) {
System.println("%{e.type()}: %{e.what()}");
throw;
} finally {
System.println("Finally");
}
Error: Can not use throw without expression outside catch clause near the <test.kx>:2
try {
throw RuntimeException("Some error occurred");
} catch (e) {
System.println("%{e.type()}: %{e.what()}");
throw; // e will be re-thrown.
} finally {
System.println("Finally");
}
RuntimeException: Some error occurred
Finally
Uncaught exception: No one catch the exception.
RuntimeException: Some error occurred
Stack Trace Information:
at <main-block>(test.kx:2)