Try-Catch-Finally Statements Syntax:
try {
//Perform some operation that might cause an exception.
} catch (exception e){
//Handle generic exception here.
} catch (dmlexception de){
try {
//Perform some operation that might cause an exception.
} catch (exception e){
//Handle generic exception here.
} catch (dmlexception de){
//Handle dml exception here.
} finally {
//this block of code always execute and allows you to clean up your code.
}
Apex Code
try {
// Perform some operation that might cause an exception.
Merchandise__c m = new Merchandise__c();
insert m;
// This doesn't execute since insert causes an exception
System.debug('Statement after insert.');
} catch (Exception e) {
// Handle generic exception here.
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
} finally {
// Perform some clean up.
}
//this block of code always execute and allows you to clean up your code.
}
Apex Code
try {
// Perform some operation that might cause an exception.
Merchandise__c m = new Merchandise__c();
insert m;
// This doesn't execute since insert causes an exception
System.debug('Statement after insert.');
} catch (Exception e) {
// Handle generic exception here.
}catch(DmlException e) {
System.debug('The following exception has occurred: ' + e.getMessage());
} finally {
// Perform some clean up.
}
No comments:
Post a Comment