例外処理の例
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Merchandise__c m = new Merchandise__c();
18insert m;この例の insert DML ステートメントは、必須項目を設定せずに商品品目を挿入しているため、DMLException を発生させます。この例外エラーは、デバッグログに次のように表示されます。
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Description, Price, Total Inventory]: [Description, Price, Total Inventory]
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17try {
18 Merchandise__c m = new Merchandise__c();
19 insert m;
20} catch(DmlException e) {
21 System.debug('The following exception has occurred: ' + e.getMessage());
22}開発者コンソールに表示される要求の状況は、正常に完了したことを報告しています。これは、コードが例外を処理しているためです。
例外の後に出現する try ブロックのステートメントはすべてスキップされ、実行されません。たとえば、insert m; の後にステートメントを追加しても、ステートメントは実行されません。次のコードを実行します。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17try {
18 Merchandise__c m = new Merchandise__c();
19 insert m;
20 // This doesn't execute since insert causes an exception
21 System.debug('Statement after insert.');
22} catch(DmlException e) {
23 System.debug('The following exception has occurred: ' + e.getMessage());
24}新しいデバッグログエントリには、「Statement after insert」というデバッグメッセージは表示されません。これは、この debug ステートメントが挿入で発生した例外の後に出現し、実行されないためです。例外が発生した後にコードステートメントの実行を続行するには、try-catch ブロックの後にステートメントを配置します。この変更されたコードスニペットを実行すると、デバッグログに「Statement after insert」というデバッグメッセージが表示されるようになります。
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17try {
18 Merchandise__c m = new Merchandise__c();
19 insert m;
20} catch(DmlException e) {
21 System.debug('The following exception has occurred: ' + e.getMessage());
22}
23// This will get executed
24System.debug('Statement after insert.');
25または、try-catch ブロックを追加できます。このコードスニペットでは、2 つ目の try-catch ブロック内に System.debug ステートメントがあります。これを実行すると、前と同じ結果になります。
1swfobject.registerObject("clippy.codeblock-4", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17try {
18 Merchandise__c m = new Merchandise__c();
19 insert m;
20} catch(DmlException e) {
21 System.debug('The following exception has occurred: ' + e.getMessage());
22}
23
24try {
25 System.debug('Statement after insert.');
26 // Insert other records
27}
28catch (Exception e) {
29 // Handle this exception here
30}finally ブロックは、発生した例外に関係なく、また例外が発生しなくても常に実行されます。実際にどう使用されるのか見てみましょう。次のコードを実行します。
1swfobject.registerObject("clippy.codeblock-5", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Declare the variable outside the try-catch block
18// so that it will be in scope for all blocks.
19XmlStreamWriter w = null;
20try {
21 w = new XmlStreamWriter();
22 w.writeStartDocument(null, '1.0');
23 w.writeStartElement(null, 'book', null);
24 w.writeCharacters('This is my book');
25 w.writeEndElement();
26 w.writeEndDocument();
27
28 // Perform some other operations
29 String s;
30 // This causes an exception because
31 // the string hasn't been assigned a value.
32 Integer i = s.length();
33} catch(Exception e) {
34 System.debug('An exception occurred: ' + e.getMessage());
35} finally {
36 // This gets executed after the exception is handled
37 System.debug('Closing the stream writer in the finally block.');
38 // Close the stream writer
39 w.close();
40}上記のコードスニペットでは、XML ストリームライタを作成し、いくつかの XML 要素を追加します。次に、null の String 変数 s にアクセスしたために例外が発生します。catch ブロックがこの例外を処理します。続いて、finally ブロックが実行されます。このブロックでデバッグメッセージが書き出され、ストリームライタが終了し、それによって関連リソースが解放されます。デバッグログでデバッグ出力を確認します。例外エラーの後にデバッグメッセージ「Closing the stream writer in the finally block.」が表示されます。これにより、例外がキャッチされた後に finally ブロックが実行されたことがわかります。