| try ... catchについて |
| try catchのエラーについて |
| 1.構文エラ(文法エラー) |
| プログラムの実装をした際に作ってしまったエラーです。 キーワードのタイプミスやカッコの閉じる個数の過不足などがあります。 |
| 2.論理エラー |
| プログラムは実行できても仕様にそぐわない結果を実行してしまう結果のプログラムです。 原因としては変数に設定する値や条件のミスや条件分岐のタイミングによるエラーがあります。 |
| (1) name |
| エラーの種類を表す文字列です。 (例) TypeError ReferenceError |
| (2) message |
| 詳細なエラーメッセージです。 (例) "Cannot read property 'xxx' of undefined". |
| (3) stack |
| エラーが発生した場所を示すスタックトレースを表示します。 copy
try {
//エラーが発生すると仮定します
let result = test();
} catch (error) {
//エラーが発生した場合の処理
console.error(error.name);//(例)TypeError
console.error(error.message);//(例)Cannot read property 'xxx' of undefined.
console.error(error.stack);//(例)スタックトレース
}
|
class ValidationError extends Error
{
constructor(message, value)
{
super(message);
this.name = 'ValidationError';
this.field = field;
}
}
let range = 0;
...
try
{
//バリデーションエラーを発生させる
throw new ValidationError('データ範囲外', range);
} catch (error) {
if (error instanceof ValidationError)
{
console.error(error.name);//(例)ValidationError
console.error(error.message);//(例)データ範囲外
console.error(error.value);//(例)rangeの値
}
}
function test1()
{
// 'result' は宣言されていません
console.log(result);//ReferenceError: result is not defined
}
test1();
スコープ外の変数を参照しようとしたケース
let data1 = 1;
function test2() {
console.log(data1);//1(スコープ内)
console.log(data2);//ReferenceError: data2 is not defined (スコープ外)
}
test2();
//undefined のオブジェクトのプロパティにアクセス
let obj = undefined;
try
{
console.log(obj.prop);//ReferenceError: Cannot read properties of undefined (reading 'prop')
}catch (e){
console.log(e);
}
function test()
{
const ary=[];
for(i=0;i<3;i++)
{
ary[i]=i+1;
}
//下記の部分では要素不足のためエラーが発生します
if(ary[3]==3)
{
...
}
}
test();
const num = "100"; const sum = num + 10;//TypeError: 文字列と数値の加算は型エラー const obj = null; obj.property = "aaa";//TypeError: nullオブジェクトのプロパティにアクセス
try
{
decodeURI('%'); // %は単独では不正なエンコード
} catch (e) {
if (e instanceof URIError) {
console.log("URIErrorが発生しました:", e.message);
}
}
try {
eval("1 + 1"); // 通常は問題ない
eval("throw new Error('test')"); // エラーが発生
} catch (e) {
if (e instanceof EvalError) {
console.log("EvalErrorが発生しました。");
} else {
console.log("EvalError以外のエラーが発生しました。", e);
}
}
| about try ... catch |
| about try ... catch error |
| 1. Syntax error (grammar error) |
| These are errors that were made when implementing the program. These include typos in keywords and too many or too few closing parentheses. |
| 2. Logical Error |
| Even if the program can be executed, it will end up executing results that do not match the specifications. The causes of this include errors in the values or conditions set for variables, or errors due to the timing of conditional branching. |
| (1) name |
| A string that indicates the type of error. (Example) TypeError ReferenceError |
| (2) message |
| A detailed error message. (Example) "Cannot read property 'xxx' of undefined". |
| (3) stack |
| Displays a stack trace showing where the error occurred. copy
try {
//Assuming you get an error
let result = test();
} catch (error) {
//What to do if an error occurs
console.error(error.name);//(例)TypeError
console.error(error.message);//(例)Cannot read property 'xxx' of undefined.
console.error(error.stack);//(例)Stack trace
}
|
class ValidationError extends Error
{
constructor(message, value)
{
super(message);
this.name = 'ValidationError';
this.field = field;
}
}
let range = 0;
...
try
{
//Raising Validation Errors
throw new ValidationError('Data out of range', range);
} catch (error) {
if (error instanceof ValidationError)
{
console.error(error.name);//ex.)ValidationError
console.error(error.message);//ex.)Data out of range
console.error(error.value);//ex.)range value
}
}
function test1()
{
//'result' is undeclared
console.log(result);//ReferenceError: result is not defined
}
test1();
Cases where an attempt is made to reference a variable outside of its scope
let data1 = 1;
function test2() {
console.log(data1);//1(In scope)
console.log(data2);//ReferenceError: data2 is not defined (Out of scope)
}
test2();
//Accessing properties of an undefined object
let obj = undefined;
try
{
console.log(obj.prop);//ReferenceError: Cannot read properties of undefined (reading 'prop')
}catch (e){
console.log(e);
}
function test()
{
const ary=[];
for(i=0;i<3;i++)
{
ary[i]=i+1;
}
//An error occurs in the following part due to a lack of elements.
if(ary[3]==3)
{
...
}
}
test();
const num = "100"; const sum = num + 10;//TypeError: Adding a string and a number causes a type error const obj = null; obj.property = "aaa";//TypeError: Accessing properties of a null object
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |