Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. "missing return statement" error [Solved]

"missing return statement" error [Solved]

Scheduled Pinned Locked Moved Java
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    I have a function that is similar to the following:

    public int LoadFromFile(String fileName) {
    FileReader reader = null;
    try {
    int rval = 0;
    reader = new FileReader(fileName);
    // Do some stuff to get 'rval' from the file
    return rval;
    } finally {
    if (reader != null) reader.close();
    }
    }

    I don't understand why I am getting the compile-time error, "missing return statement". Either execution gets to the line "return rval;", or an exception is thrown and no value needs to be returned. I can get the compile-time error to go away if I move the return value declaration prior to the try block and move the return statement after the finally block (I read online that putting a return statement within the finally block suppresses all exceptions), but I don't understand why I need to do that. Thanks for any help,

    Sounds like somebody's got a case of the Mondays -Jeff

    S 1 Reply Last reply
    0
    • S Skippums

      I have a function that is similar to the following:

      public int LoadFromFile(String fileName) {
      FileReader reader = null;
      try {
      int rval = 0;
      reader = new FileReader(fileName);
      // Do some stuff to get 'rval' from the file
      return rval;
      } finally {
      if (reader != null) reader.close();
      }
      }

      I don't understand why I am getting the compile-time error, "missing return statement". Either execution gets to the line "return rval;", or an exception is thrown and no value needs to be returned. I can get the compile-time error to go away if I move the return value declaration prior to the try block and move the return statement after the finally block (I read online that putting a return statement within the finally block suppresses all exceptions), but I don't understand why I need to do that. Thanks for any help,

      Sounds like somebody's got a case of the Mondays -Jeff

      S Offline
      S Offline
      Skippums
      wrote on last edited by
      #2

      The above code does work. Turns out I had the return statement within a while loop on accident, so if the condition was initially false I wouldn't have thrown an error nor returned a value.

      Sounds like somebody's got a case of the Mondays -Jeff

      L 1 Reply Last reply
      0
      • S Skippums

        The above code does work. Turns out I had the return statement within a while loop on accident, so if the condition was initially false I wouldn't have thrown an error nor returned a value.

        Sounds like somebody's got a case of the Mondays -Jeff

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        :thumbsup:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups