Samantha J T Star
Samantha J T Star

Reputation: 32808

Is there a "clean" way to jump out of a "try" block without an exception in C#

I have the following code.

        try
        {
            if (vm.SubmitAction == "Cancel")
                return RedirectToAction("ShowSummary", new
                {
                    ds = vm.Meta.DataSourceID
                 });  <------------------------------------------- xxxx
            _account.ValidateNoDuplicate(vm.Account);
            vm.Account.Modified = DateTime.Now;
            vm.Account.ModifiedBy = User.Identity.Name;
            _account.AddOrUpdate(vm.Account);
        }
        catch (Exception e) { 
            log(e); return View("CreateEdit", vm); 
        }
        return RedirectToAction("ShowSummary", new {
            ds = vm.Meta.DataSourceID
        });

If the user hits the cancel button then I have code ( Marked here with <-- xxxx ) that redirects to an action. This code is the same as the code after the try block. Is there a way that I can make my code exit from the try. The only way I can think of is to trigger an exception and I want a clean jump and not one that uses the exception as it's not an error for a user to click cancel.

Upvotes: 2

Views: 2854

Answers (3)

St&#233;phane Bebrone
St&#233;phane Bebrone

Reputation: 2763

There is a way to exit a try catch manually (with a goto statement) but that's a terrible OO practice and should be avoided.

Encapsulate your RedirectToAction logic into a method and call it:

try
{
    if (vm.SubmitAction == "Cancel")
        return ShowSummary(vm);
    _account.ValidateNoDuplicate(vm.Account);
    vm.Account.Modified = DateTime.Now;
    vm.Account.ModifiedBy = User.Identity.Name;
    _account.AddOrUpdate(vm.Account);
}
catch (Exception e) { 
    log(e); 
    return View("CreateEdit", vm); 
}
return ShowSummary(vm);

And the method:

private ActionResult ShowSummary(MyViewModel vm)
{
    return RedirectToAction("ShowSummary", new
           {
               ds = vm.Meta.DataSourceID
           });  
}

Upvotes: 3

oqx
oqx

Reputation: 2377

I know it is not the best programming keyword but goto do the job

try
    {
        if (vm.SubmitAction == "Cancel")
            goto ShowSummary;

        _account.ValidateNoDuplicate(vm.Account);
        vm.Account.Modified = DateTime.Now;
        vm.Account.ModifiedBy = User.Identity.Name;
        _account.AddOrUpdate(vm.Account);
    }
    catch (Exception e)
    {
        log(e); return View("CreateEdit", vm);
    }
ShowSummary:
    return RedirectToAction("ShowSummary", new
        {
            ds = vm.Meta.DataSourceID
        });

Upvotes: 2

Thilo
Thilo

Reputation: 262514

Switch the if block condition around:

// happens only when not cancelled
if (vm.SubmitAction != "Cancel") 
  try {
     _account.ValidateNoDuplicate(vm.Account);
     vm.Account.Modified = DateTime.Now;
     vm.Account.ModifiedBy = User.Identity.Name;
     _account.AddOrUpdate(vm.Account); 
   }
   catch (Exception e) { 
       log(e); return View("CreateEdit", vm); 
   }

// happens always
return RedirectToAction("ShowSummary", new {
       ds = vm.Meta.DataSourceID
});

Upvotes: 5

Related Questions