Results 1 to 2 of 2
  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Try- catch block + return + IO package

    Hi,

    I have a doubt in usage of return stmt in try-catch block. I have a function in which i read a file and that i stored in a String array. All this part will be written in try-catch block. I would like return that string array. Where I have to put this return stmt.

    public int[] readDataFromAFile()
    {
    String tkn[]=null;
    try
    {
    BufferedReader br = new BufferedReader(new FileReader("PrintOutput.txt"));
    String line;
    while((line=br.readLine())!=null)
    {
    tkn =line.split(",");
    for(int i=0; i<tkn.length;i++)
    System.out.println(tkn[i]);
    }
    }
    catch(Exception e)
    {
    System.out.println("Exception: " + e);
    }
    return tkn;
    }

    By running this code it is showing 2 errors.
    1st error at At catch block.
    2nd is at return stmt.

    Invalid expression statement.
    {
    ^
    error: '{' expected.
    return tkn;
    ^

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Syntax error is a different issue, but what exactly are you supposed to return from this method? Because your method signature says return type is array of int, however what you are returning i.e. tkn is a array of String.

Similar Threads

  1. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  2. System.exit() in catch block.
    By new_2_java in forum Advanced Java
    Replies: 8
    Last Post: 06-24-2008, 03:45 PM
  3. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 07:10 AM
  4. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM
  5. Return to try block
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-11-2007, 08:58 PM

Posting Permissions

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