Java naming convention - using the "try" prefix to separate exception handling

According to Robert C. Martin's “Clean Code,” exception handling should be performed by a separate method. In one example, the author uses the prefix "try" (or "tryTo") for a separate public method and a private method that executes the corresponding logic, as shown below.
public void doSomething() {
    try {
        tryToDoSomething();
    catch (Exception ex) {
        //handle exception
    }
}

public void tryDoSomething() throws Exception {
    // appropriate logic
}

Is this the right deal? Maybe method names should be inverse? Would a "try" prefix method be better that contains a try-catch block as shown below (similar to C #)?

public void tryDoSomething() {
    try {
        doSomething();
    catch (Exception ex) {
        //handle exception
    }
}

public void doSomething() throws Exception {
    // appropriate logic
}

In the first approach, the name of the public method is smaller and more understandable. On the other hand, to make the code consistent, I must prefix each method that throws an exception. Which agreement do you prefer?

What name do you prefer for exception variables in the try-catch block - exception, ex, e? This is a detail, but I'm really curious.;)
+4
source share

All Articles