Given the code below, how would you add exception handling in your code?
public void ButtonClicked() {
DoSomething();
DoTask2();
}
private void DoSomething() {
DoSomethingElse();
}
private void DoSomethingElse() {
// code
}
private void DoTask2() {
// code
}
Some developer would handle like this
public void ButtonClicked() {
try {
DoSomething();
DoTask2();
}
catch(Exception ex) {
logger.LogError(ex, "Error in buttonclicked");
}
}
private void DoSomething() {
DoSomethingElse();
}
private void DoSomethingElse() {
// code
}
private void DoTask2() {
// code
}
While other do something like this
public void ButtonClicked() {
var result = DoSomething();
if (result.Success) {
var task2Result = DoTask2();
if (!task2Result.Success) {
logger.logError(result.Error);
}
}
else {
logger.logError(result.Error);
}
}
private Result DoSomething() {
try {
DoSomethingElse();
}
catch(Exception ex) {
return new Result { Success = false, Error = ex.Message }
}
}
private result DoSomethingElse() {
try {
// code
}
catch(Exception ex) {
return new Result { Success = false, Error = ex.Message }
}
}
private void DoTask2() {
try {
// code
}
catch(Exception ex) {
return new Result { Success = false, Error = ex.Message }
}
}
public class Result
{
public bool Success { get; set; }
public string Error { get; set; }
}
Which style is more readable?