Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- if (!SendEmail()) {
- throw "Could not send e-mail";
- }
- if (!ArchiveReportsInDatabase()) {
- throw "Could not archive reports in database";
- }
- if (!CreateAFile()) {
- throw "Could not create file";
- }
- ...
- } catch (Exception) {
- LogError(Exception);
- ...
- }
- try {
- SendEmail();
- ArchiveReportsInDatabase();
- CreateAFile();
- ...
- } catch (Exception) {
- LogError(Exception);
- ...
- }
- class Compensator {
- Action Action { get; set; }
- Action Compensate { get; set; }
- }
- Queue<Compensator> actions = new Queue<Compensator>(new Compensator[] {
- new Compensator(SendEmail, UndoSendEmail),
- new Compensator(ArchiveReportsInDatabase, UndoArchiveReportsInDatabase),
- new Compensator(CreateAFile, UndoCreateAFile)
- });
- Queue<Compensator> doneActions = new Queue<Compensator>();
- while (var c = actions.Dequeue() != null) {
- try {
- c.Action();
- doneActions.Add(c);
- } catch {
- try {
- doneActions.Each(d => d.Compensate());
- } catch (EXception ex) {
- throw new OhCrapException("Couldn't rollback", doneActions, ex);
- }
- throw;
- }
- }
- try {
- task1();
- task2();
- task3();
- ...
- taskN();
- }
- catch (TaskFailureException e) {
- dealWith(e);
- }
- try:
- SendEmail()
- try:
- ArchiveReportsInDatabase()
- try:
- CreateAFile()
- except:
- UndoArchiveReportsInDatabase()
- raise
- except:
- UndoSendEmail()
- raise
- except:
- // handle failure
- SendEmail () {
- # ...
- }
- ArchiveReportsInDatabase () {
- # ...
- }
- CreateAFile () {
- # ...
- }
- SendEmail && ArchiveReportsInDatabase && CreateAFile
- return SendEmail() && ArchiveResportsInDatabase() && CreateAFile();
Add Comment
Please, Sign In to add comment