Apex DmlExceptionの使い方のサンプル
環境
Salesforce
概要
DmlException
insert ステートメントでレコードの必要な項目が欠落している場合など、
DML ステートメントに関する問題を示す例外。
操作例
insert DML ステートメントは、必須項目を設定せずに商品品目を挿入しているため、
DMLException を発生させます。この例外は、catch ブロックでキャッチされ、
System.debug ステートメントを使用して例外メッセージがデバッグログに出力されます。
サンプルコード
try {
Merchandise__c m = new Merchandise__c();
insert m;
} catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
try {
Merchandise__c m = new Merchandise__c();
insert m;
} catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
try { Merchandise__c m = new Merchandise__c(); insert m; } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); }