Doubts reg try block and return statements

hi
public int test()
          try
               System.out.println("hi");
               return 1;
          catch(Exception e)
          System.out.println("err");
          return 2;
          finally
               System.out.println("final");
               return 3;
          //System.out.println("after");
          //return 4;
when i call this function, it will printing 3... why ??
and also compilation error is coming when i put return statement after final block.. why is it so??

but if commented the return statements in catcch and finally then it iam not getting any compilation error.
public int test()
          try
               System.out.println("hi");
               return 1;
          catch(Exception e)
          System.out.println("err");
          // return 2;
          finally
               System.out.println("final");
          //     return 3;
          System.out.println("after");
          return 4;
and iam just learning ..... regarding return and try block

Similar Messages

  • Reg Price block and Qty block in MM Invoice docs Tolerence Keys adjustment

    Hi Friends,
    Related to MM Invoice documents gets blocked with Price block and Qty block,based on this FI doc also gets blocked with Payment block indicator"R".As per Std SAP we can remove these blocks thru MRBR. Right now my users are asking not required to remove these blocks with MRBR Tcode.
    So i checked the Tolerence Limits defined for the co code and Tolerence keys.
    As per my understanding Tolerence Keys "PP-Price variance",  "DQ-Exceed amount: quantity variance" need to adjust with upper llimits and Lower Limits.
    here i can see Upper and Lower check limit Tolerance limit 5.00%  for  Tolerence Key "PP-Price variance"
    Tolerence limit 0.01%  for  Tolerence key "DQ-Exceed amount: quantity :quantity variance         
    Instead of changing the Tolerence Limits can i select the DO NOT CHECK Radio button for the bothe Price and Qty block.
    If i selected the DO NOT check button what will happen,how it works.
    How i can maintain the Tolerence keys,is there any other Tolerence keys also need to maintain.  
    Please suggest me with your valuable inputs.  
    Brgds,
    RK
    Edited by: RK.SAP on Nov 17, 2010 7:02 AM

    hi,
    If you do not want to check the Invoice for tolerance keys like quantity variance and price varaince then tick on don't check .In this case system will not block any invoice and invoice will post Automatically without any error.
    Please check other tolerance keys in SPRO: OMR6
    One more blocking is defined in the vendor master also under payment transactions accounting screen.There you are defining the tolerance group for Invoice Verification.
    Regards,
    Amit

  • G/L Account Blocked and Return to vendor should be done

    hi all
    i have a scenario where i need some solution.
    two years before user created a PO with some G/L account and one year before that GL account is blocked for posting and now the user wants to do 122 movement type returns to vendor..
    is there any option where we can change the g/l account while doing or any other options?
    regards
    Ganeshkumar

    Hi,
    I think you can unlock the GL A/C..and post the movement and lock it again.
    but you need proper approval for that.
    Kuber

  • Will you have option to try IPhone and return if you do not like it?

    I am changing from Evo to Iphone and not sure if i will like it.  Will it matter if you order from apple or sprint do you get that 30 day period to return if unhappy?  Talked to sprint and they are not waiving activation fee, so if ordered thru apple do I just call sprint to activate?  thanks

    You can return it if you do not like it. Even if you buy it through Apple you will pay the Sprint activation fee.
    If you go to your nearest Apple Store you can play with an iPhone 4s and get a good idea if you will like it or not.

  • Reg : Screen Flow and Progress States /Progress Bar

    Hi All,
    I've used 'Screen Flows' for the user to navigate to different screens based on the user input.
    By doing so, I'm not been able to use Progress Stages or Progress Bar features :(
    If i remove the screen flow, then Progress Stages/ Progress Bar shows up.
    But as per the screen order, it shows up all the screens, even if I don't want to display it to the user on certain conditions.
    I'm using OPA 10.4.
    How do i display the Progress Stages/ Progress Bar ,using the screen flow..
    Any help would be greatly appreciated
    Regards,
    Kumar

    Only screen orders support progress stages. Screen flows do not.
    The recommended approach is to organize the screens into folders. It is only the top-level (screens or folders) that are then shown as the stages.
    Davin.

  • Return statement in a try catch block

    Hi friends
    Take a look in the code bellow
    try
    return (true)
    finally
    System.out.println("blabla");
    If nothing strange hapens in the try code, the return statement will be achieved. In this case, what will hapen ? The code in the finally block will be executed ??
    thanks

    If nothing strange hapens in the try code, the return
    statement will be achieved. In this case, what will
    hapen ? The code in the finally block will be executed
    ??Yes. Hence the word "finally".

  • Return statement should put beyond try/catch clause??

    The return statement should put beyond the try/catch clause, is that correct? Well,
    I tried to put inside the try block, but it will have compile error though.
    public String getValue()
         String value;
         try
         catch(...)
         return value;
    please advise. thanks!!

    When a method returns a value, you must make sure that even if an exception is thrown and caught a value will be returned (or just throw the exception out of the method).
    You can put a return clause as the last thing in the try block and another return clause after the catch block (this is where we go if we catch an exception so you probably want to return null).

  • Why its not a good idea to have a return statement in a finally block?

    deepak

    Finally executes after the try block has finished doing its work--either successfully or not. Finally is only for cleanup that has to be run after try finishes, regardless of how try completes. It's up to the try block to handle the business logic, the real work.
    If the real work in the try block says, "return x," then it is not appropriate for finally--the cleanup code--to alter that with "return y" or "throw new Exception()".
    If the real work in the try block throws an exception, it is not appropritae for the cleanup code to throw a different exception, or change that to a return.
    Remember, if you throw or return from within finally, that will superceded whatever your mode of completion in the try block was.

  • RETURN STATEMENT IN A PROC PROGRAM

    HI,
    I have written a small program in proc which is supposed to pick
    up records from a table matching a certain criteria.The records
    will be displayed in the screen one after the other. For every
    record displayed there is an option to perform certain
    operations such as
    (1) authorise the record which will update certain values to
    certain fields in the table
    (2) skip the record which would display the next record from the
    cursor
    (3) Reject the record which will delete it from the table and
    then fetch the next record from the cursor
    (4) quit - which should return control out of the function to
    the place where the function was called. This has been done by
    way of a return statement.
    There are only two ways to come out of the function
    (1) one is when the quit option is chosen as explained above
    (2) when the control goes past the last record in the cursor
    (when 1403 is raised)
    However, when the above two conditions are met (for eg I skip
    all the records and the reach the end of the cursor) or when i
    explicity choose the quit option, the control does not come out
    of the function, but keeps calling the function repeatedly .
    The only time when it comes out is when the cursor does not
    fetch any records.
    The program works perfectly fine in a solaris machine (oracle
    version 8.1.7). The problem is faced in a DEC ALPHA machine
    with an oracle 8.0.6 version.
    Has this anything to do with the machine set up, compilation
    script etc...or pls suggest what could be the other
    possibilities.
    Please note that the return statements used in the sub functions
    are working properly.
    Regards...Rajee

    If nothing strange hapens in the try code, the return
    statement will be achieved. In this case, what will
    hapen ? The code in the finally block will be executed
    ??Yes. Hence the word "finally".

  • Help on "try block"

    hi there
    if the try block catches an exception, its catch block will execute, will the code return to the beginning of the try block or exit the try block and execute the next statement? if I use a try block to validate user input, will it keep trying until the input is valid, or I should use a loop. thank you .

    When the code within the try block generates an
    exception, the code in the catch block is executed and
    then processing continues with the next statement
    after the catch block.
    Mark

  • How to catch errors in calling program when using SUBMIT ... AND RETURN ?

    Hi ,
    I am calling a report using submit and return statement from a function module. The report has select-options field for a date field. If user enters a date in low, which is greater than high, getting error message "Lower limit is greater than upper limit".
    How to catch this error without endup stoping on selection screen of the report?
    In the documentation, I read :
    Programs called with SUBMIT ... AND RETURN or CALL TRANSACTION cannot propagate exceptions to the caller, because exception objects are bound to the internal mode of a program.
    Is there any way to overcome this?
    Also my aim is to write a validation on the date selection-option to have the date range not more than 20 days.
    I am not sure of how to achieve this. 
    Is there a way to do validation of select-options data before submiting to the report ?
    Is there any standard FM to achieve this task?
    Thank you,
    Surya.
    Edited by: Surya on May 24, 2009 6:08 PM

    Hi Surya ,
    In your function module's code validate the date .
    If the low value of the date less than high and date period is not more than 20 then
    call transaction and in else condition raise exception.
    In your FM write like that - -
    IF   date-low LT date-high AND
                           date_difference LE '20' .
    CALL  TRANSACTION   'transaction'.
    ELSE.
    RAISE  ...........  " Raise the invalid date exception
    ENDIF.
    Regards
    Pinaki

  • Return statement inside try block

    what is wrong if i write code like as below...please explain me since i am new to java
    class sample{
    public String method(){
    try{
    String str="abc";
    return abc;
    catch(Exception e){}
    }

    veldhanas wrote:
    return abc;In your code there is no varible declared as abc. It is a value assigned in str.
    Suppose if the code in try statement throws exception the return statement is skipped and the
    associated catch block will get executed. So your catch block must return a result of type String.... or throw another (or the same) exception.
    In this case, since there's no way the code in the given "try" block can throw an exception, it would have been better to not even have a try/catch block in the first place.
    And almost never just swallow exceptions like that (an empty catch block). There are only a few cases where it's ok to swallow them (such as in finally blocks where you're cleaning up resources which may throw exceptions while cleaning up, and you want to continue cleaning up and ignore those kinds of exceptions).

  • Return statement and Try Catch problem

    Hi!!
    I've got the next code:
    public ResultSet DBSelectTeam(String query) {
    try {
    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery(query);
    return rs;
    } catch (Exception err) {
    JOptionPane.showMessageDialog(null, "ERROR: " + err);
    But I need a return statement in the catch-block, but I don't know what's the best option.
    Help...
    Many thanks.

    The error message is: "missing return statement", Yes, I know.
    You have to either return from the catch statement, or throw from the catch statement, or return or throw after the catch statement.
    The only ways your method is allowed to complete is by returning a value or throwing an exception. As it stands, if an exception is thrown, you catch it, but then you don't throw anything and you don't return a value.
    So, like I said: What would you return from within or after catch? There's no good value to return. The only remotely reasonable choice would be null, but that sucks because now the caller has to explicitly check for it.
    So we conclude that catch shouldn't return anything. So catch must throw something. But what? You could wrap the SQLE in your own exception, but since the caller is dealing with JDBC constructs anyway (he has to handle the RS and close it and the Statement), there's no point in abstracting JDBC away. Plus he has to deal with SQLE anyway in his use of the RS and Statement. So you might as well just throw SQLE.
    So since you're going to just throw SQLE anyway, just get rid of the try/catch altogether and declare your method throws SQLException

  • Return statement at the end of try or after catch blocks

    Hi
    Can anyone tell me which is the better practice - to put the return statement at the end of try block or after all the catch blocks ie at the end of method.
    Eg
    Method A()
    String str;
    try{
    str= [some code]
    return str
    catch(Exception e)
    System.out.println("Exception");
    } // end of method
    OR
    Method B()
    String str;
    try{
    str= [some code]
    }catch(Exception e)
    System.out.println("Exception");
    return str
    } // end of method

    I always try to work with only one exit point for each method.
    For readability I always put the return statement as close to the end of the method as possible.
    In this particular question,
    I think you should put the return at the end of the method (for readability, since this is what you are
    familiar with), but when I have a try-catch clause, I usually have an unrecoverable error and
    should throw this further down the tree, so it usually becomes
    try{
       return   
    }catch(...){
       // write out some logging information
       rethrow exception or throw another exception
    }I think you should NEVER reflect the occurence of an error in the return-value of the
    method when an unrecoverable exception occurs. Just rethrow this exception or throw another method.
    Other methods look like
       Object result = new...
       return result;As for the specific case of a repetitive if-case:
    This is possible in two versions:
    With one return and a result-object
    Object result = new ...
    if(..)
       result = ...
    else if(..)
       result = ...
    return result;With every time a return
    if(...)
       return ...;
    else if(...)
       return ...;For me the above two possibilities make no difference,
    but I find the second version (which I hated when I began programming)
    to be somewhat more of a (self-made) standard nowadays.
    But I do not think this particular case makes much of a difference.
    kind regards,

  • Return statement in exception and consequences

    Hi,
    Can any one give me the explanation for my doubt?
    Case 1:
    I am having some code throwing some exception lets say arithematic exception in my try block.
    I caught it in the catch block and at last I am having one finally?
    as per normal order try block next catch block and at last finally will execute.
    I execute the above code by putting a return statement inside catch block.I got the same order of execution irrespective of return statement inside catch?
    What is the significance of return inside catch?
    Case 2:
    Lets take the scenario.
    class MyException
         public static void main(String[] args)
              int i=1,j=0;
              try
                   try
                   System.out.println("the value is: "+(i/j));
                   System.out.println("Inside try block");
                   catch(ArithmeticException e)
                   System.out.println("hi in ame");
                   //return;
              finally
              System.out.println("inner finally");
                                            System.out.println("outer try");
              catch(Exception e)
                             e.printStackTrace();
                   System.out.println("in exception");
              finally
                   System.out.println("plz wait");
    If return statement is there inside the inner try and catch the code out of inside outer try not getting executed Why So?
    Any clarifications?
    Thanking you,
    Murthy.

    First, please format your code as per http://forum.java.sun.com/features.jsp#Formatting
    I'm not sure what part you don't understand.
    .    public static void main(String[] args) {
    .        try {
    .            try {
    .                throw new ArithmeticException();
    .                System.out.println("Inside try block"); // #1
    .            catch(ArithmeticException e) {
    .                System.out.println("hi in ame"); // #2
    .                return;
    .            finally {
    .                System.out.println("inner finally"); // #3
    .            System.out.println("outer try"); // #4
    .        catch(Exception e) {
    .            System.out.println("in exception"); // #5
    .        finally {
    .            System.out.println("plz wait"); // #6
    .    }#1 -- You won't get here because you throw AME.
    #2 -- You will get here because you caught the AME you just threw.
    #3 -- You will get here because it's a finally
    #4 -- You won't get here. I think that this is what you're asking about: Why don't we get here? Because we've already done 'return' and this line is NOT inside a finally. The only things that can get executed after a return are finally blocks.
    #5 -- You won't get here because you already caught the AME and didn't rethrow it or any other exception.
    #6 -- You will get here because it's a finally.
    Once you do a return, the only code you can execute before exiting the method are finally blocks. First the one corresponding to the try/cathc block where your return is, then the finally for the try/catch that encloses that one, and so on outward. The code between one finally and the next (#4 here) is NOT executed.
    If you still have a question, please try to clarify exactly what behavior you don't understand.
    Don't return from inside t/c/f.

Maybe you are looking for