• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we have return statement in try block ?  RSS feed

 
Harnoor Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

I am trying the following program :-

  • public class Galti {

    private int var1 ;
    public int calc(int j, int k){
    try{
    var1 = j/k;
    return var1;
    }catch(Exception ex){
    System.out.println("Check");
    }
    }
    }

  • Above program is giving complication error :- "Method must return a result of type int".

    Regards,
    Harnoor


     
    Wouter Oet
    Bartender
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you.

    First of all try and experiment. That way you will learn much more about the language.
    I'll try to answer your question with a question: what does the method return in case the exception happens?
     
    Harnoor Singh
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Harnoor Singh
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "what does the method return in case the exception happens"


    It will not return anything. It means we can have return statement in the finally block but not in try and catch block? kindly let me know if this is the case.
     
    Wouter Oet
    Bartender
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Harnoor Singh wrote:It will not return anything.
    But you declared that it did return something so that is not an option. I said it before and I'll say it again. Try and experiment.
     
    Eduardo Yañez Parareda
    Ranch Hand
    Posts: 93
    IntelliJ IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Harnoor Singh wrote:
    "what does the method return in case the exception happens"


    It will not return anything. It means we can have return statement in the finally block but not in try and catch block? kindly let me know if this is the case.


    I'm sorry, but I think you haven't tested your method because it can't compile. First of all, you can put a return statement within try block, that's not the problem, and it's totally correct.

    Second, try to compile it and see the error the compiler gives you.
     
    T Dahl
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is a simpler piece of code that has the same problem:

    Can you figure out why I get the same error message at compile time? If so, check to see if your problem could be similar!
     
    Baichuan Huang
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Harnoor Singh
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all for clearing my doubt.

    Learning from this thread:- We need to make sure that return statement should be reachable code for every case.
     
    Wouter Oet
    Bartender
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Baichuan Huang

    I would really not code like that. If an exception is thrown then the last calculated value is returned or if it's the first calculation then 0. Which makes the output unpredictable. Also the use of an instance variable makes it not threadsafe and you can check for the condition instead of trying and catching the Exception which should significantly increase the performance.
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Harnoor Singh wrote:
    "what does the method return in case the exception happens"


    It will not return anything. It means we can have return statement in the finally block but not in try and catch block? kindly let me know if this is the case.

    Have a look on this
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!