0

This is a follow-up to my previous, duplicate question Why is NetBeans saying "incompatible thrown types IOException in method reference"? [duplicate]. Why does NetBeans tell me "unreported exception IOException; must be caught or declared to be thrown" when I catch the IOException?

Screenshot of error

Screenshot of error

Full method code

/**
 * Either extracts or returns the pre-extracted language file for the given locale
 * 
 * @param locale the locale 
 * @return
 * @throws IOException 
 */
public static InputStream getLanguageFile(Locale locale) throws IOException
{
    File extracted = SaveConstants.inventSaveFileFor(Main.GAME_NAME, locale.toLanguageTag() + ".lang");
    if (!extracted.exists())
    {
        InputStream in = Resources.getLanguageStream(locale);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        extracted.getParentFile().mkdirs();
        try
        {
            extracted.createNewFile();
            Writer writer = new FileWriter(extracted);
            reader
                .lines()
                .forEach((str) -> writer.write(str));
        }
        catch (IOException ex)
        {
            Logger.getLogger(Lang.class.getName()).log(Level.SEVERE, "Cannot extract language file for " + locale + ". Loading directly to map", ex);
            return in;
        }
    }
    else
        return new FileInputStream(extracted);
}

Note: I know it won't compile because of a missing return, but I want to get this down before I move on to the return

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Don’t open a new question, try to understand the contents of the linked question. It doesn’t matter whether you write it as method reference or lambda expression. `Consumer.accept` does not allow `IOException` to be thrown. – Holger Sep 23 '14 at 16:28
  • @Holger sorry, I'm new to Java 8's way of doing lambdas and all this jargon is foreign to me. I couldn't make heads or tails of the other question's answer and I couldn't think of another way to get a direct solution/explanation of my problem than to just make another question – Ky - Sep 23 '14 at 16:36
  • You might extract solutions from [this answer](https://stackoverflow.com/a/25083160/2711488) or [that answer](https://stackoverflow.com/a/23916609/2711488)… – Holger Sep 23 '14 at 16:41
  • A lambda expression is not a method invocation. It's a body of code to be executed. – Sotirios Delimanolis Sep 23 '14 at 16:48

0 Answers0