Question

I have searched for some answers , but could not find & hence I am raising this as a new question. The catch method in the try-catch. Is it a method ? , Its taking an object of type Exception as an argument. But if its a method ,

  1. Who calls this method and by which object this method is called ?

  2. You cannot write a method definition inside another method definition normally.So if its a method , how am I able to write the catch inside another method ?

  3. If its not a method , then what is it ?

     public void foo() {
    
         try { ; 
    
         } catch (Exception ex) {
    
              ; // this is catch method body 
          }
    
    }
    

The above code means catch method body is inside another method foo()'s body.

Was it helpful?

Solution

No, it's a special Java construct that denotes a block of code to be run if an Exception is caught, it's not a method. That block of code does take a parameter (of sorts), which is the exception to catch and then deal with - but this doesn't make it a method.

As pointed out in the comment, it's similar to, say, an if statement (you can view it as taking a "parameter" which is a boolean expression, but likewise this isn't a method either.)

OTHER TIPS

it's a language construct similar to if or while or for

Who calls this method and by which object this method is called ?

The JVM executes it if the specified exception or a derived exception is thrown. No calling.

You cannot write a method definition inside another method definition normally.

You can't do it at all, except by writing an anonymous class, but that's another story.

So if it's a method

It isn't.

how am I able to write the catch inside another method ?

Because it isn't a method.

If it's not a method , then what is it ?

It's a language construct, just like try, do, while, for, switch, case, class, interface, ... are.

The above code means catch method body is inside another method foo()'s body.

No it doesn't, because it isn't a method.

Surely all these self-contradictions make it obvious that it isn't a method?

It is a block like if, while which accepts only one parameter which is called as exception parameter.

From JLS #14.20. The try statement

A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If the try statement has a finally clause, then another block of code is executed, no matter whether the try block completes normally or abruptly, and no matter whether a catch clause is first given control.

A catch clause has exactly one parameter, which is called an exception parameter.

CatchClause:
   catch ( CatchFormalParameter ) Block

Catch is not a method. try-catch block is similar to do-while block.

do{
   statements;
} while (condition);

Each catch block is an exception handler and handles the type of exception indicated by its argument.

Indeed the try-cath is not a method. It is a construct similar to if-else. But it should be a method.

For me it is a defect in the language. A method should only do one action. Let's say you have a method called "buyBread()" that should only do the action of buying the bread. If anything goes wrong with that then handling that exception is not part of buying the bread anymore. So it should be in another method. (like calling an ambulance is not part of driving your car, it is part of what you do when driving your car fails in a very terrible way). So what you do in the "try" should never be in the same method as what you do in the "catch", but you can not do that in java.

I would like java to be like this:

buyEverythingInShoppingList(...){
...
    try {
        buyBread()
    } catch(this::solveBuyBreadFailure)
...
}

private void solveBuyBreadFailure(Exception e){
    if (e.cause.equals(NO_MONEY){
        begForBread()
    } else {
        giveUpAndCry()
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top