Results 1 to 4 of 4

Thread: Try and catch

  1. #1
    Join Date
    Sep 2002
    Posts
    456

    Arrow Unanswered: Try and catch

    Hello guys,
    Why can't I return something from a method if I have the code in try and catch, e.g.

    public String getURL()
    {
    try{
    some code here
    return "yahoo.com";
    }
    catch{
    some code here
    }


    }//end of method

    somehow it complains about the return type, any suggestions on how to make this work.

    Paul

  2. #2
    Join Date
    Jan 2003
    Location
    KL, Malaysia
    Posts
    38

    Re: Try and catch

    Originally posted by dollar489
    Hello guys,
    Why can't I return something from a method if I have the code in try and catch, e.g.

    public String getURL()
    {
    try{
    some code here
    return "yahoo.com";
    }
    catch{
    some code here
    }


    }//end of method

    somehow it complains about the return type, any suggestions on how to make this work.

    Paul
    Try putting the return statement outside the catch block. or, try this:

    [QUOTE][SIZE=1]Originally posted by dollar489
    Hello guys,
    Why can't I return something from a method if I have the code in try and catch, e.g.

    public String getURL()
    {
    String yourvar = null;

    try{
    some code here

    }
    catch{
    some code here
    }finally{

    return yourvar;
    }

    }
    Save the dugongs!

  3. #3
    Join Date
    Sep 2002
    Posts
    456

    Re: Try and catch

    Thanks for the response, but I don't want to return anything if something is wrong. Finally will execute no matter what!!!


    dollar

    [QUOTE][SIZE=1]Originally posted by tswern
    Try putting the return statement outside the catch block. or, try this:

    Originally posted by dollar489
    Hello guys,
    Why can't I return something from a method if I have the code in try and catch, e.g.

    public String getURL()
    {
    String yourvar = null;

    try{
    some code here

    }
    catch{
    some code here
    }finally{

    return yourvar;
    }

    }

  4. #4
    Join Date
    Feb 2002
    Location
    Brazil
    Posts
    8

    About your problem

    My friend,

    yol'll cannot have a method with some type instead void, without getting a result back. Your problem is that you need to put the "return" outside the Try/Catch block. Otherwise, the compiler will not be able to return something, and it is a necessity. So, the possible way could be:

    public class test {

    public java.lang.String testVar;

    /** Creates a new instance of teste */
    public test() {
    }

    public static void main(java.lang.String[] args){
    test t = new test();
    t.pushValue();
    }

    public void pushValue(){

    try{
    testVar = "oi";
    }catch(java.lang.Exception e){
    System.out.println("Wrong teste "+e.getMessage());
    }

    }

    }

    So, the variable is inicialized just, and just, when the try is respected. Thus, this could help you??

    Bye.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •