Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues in scope of variables while using try-catch in Java

Tags:

java

scope

I have a class PDF which implements an interface fileReader.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class PDF implements fileReader {
    @Override
    public byte[] readFile(File pdfDoc) {
        if (!pdfDoc.exists()) {
            System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
            return null;
        }
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        byte fileContent[] = new byte[(int) pdfDoc.length()];
        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent;
    }
}

import java.io.File;
public interface fileReader {
    <T> T readFile(File fileObject);
}

I notice that there are scope issues for variables fin.

Another implementation I made was:

public byte[] readFile1(File pdfDoc) {
        if (!pdfDoc.exists()) {
            System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
            return null;
        }
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
            byte fileContent[] = new byte[(int) pdfDoc.length()];
            try {
                fin.read(fileContent);
            } catch (IOException e) {
                System.out.println("");
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        return fileContent;
    }

But now I could not access fileContent.

How can I combine the try-catches so that I don't have scope problems? Can there be a better design approach to this problem? I have to make functions for reading three different types of file.

like image 891
Animesh Pandey Avatar asked Jun 23 '15 18:06

Animesh Pandey


5 Answers

Since Java 7 you can combine the try-catch as follows:

    FileInputStream fin = null;
    try {
        fin = new FileInputStream(pdfDoc);
        byte fileContent[] = new byte[(int) pdfDoc.length()];
        fin.read(fileContent);
    } catch (IOException | FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
    }

Which, in my opinion, makes the code cleaner and variable scopes more obvious.

like image 66
StuPointerException Avatar answered Oct 26 '22 23:10

StuPointerException


You can nest the try catch statements:

    try {
       FileInputStream fin = new FileInputStream(pdfDoc);
       byte fileContent[] = new byte[(int) pdfDoc.length()];
       try {
          fin.read(fileContent);
          return fileContent;
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
         fin.close();
       }

    } catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
    }
    return null;

Note that I added a close() in a finally clause to clean up. And also returning null is probably not what you want in case of error, but that's application specific.

like image 29
JP Moresmau Avatar answered Oct 27 '22 00:10

JP Moresmau


You can have one try with multiple catch blocks.

try {
    //do stuff
}
catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
}
catch (IOException e) {
        e.printStackTrace();
}
like image 25
DrZoo Avatar answered Oct 26 '22 23:10

DrZoo


You can modify this part:

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(pdfDoc);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }
        byte fileContent[] = new byte[(int) pdfDoc.length()];
        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }

By

{
......
       FileInputStream fin = null;
       byte fileContent[]=null;
        try {
            fin = new FileInputStream(pdfDoc);
            fileContent = new byte[(int) pdfDoc.length()];
            fin.read(fileContent);
        } catch (FileNotFoundException e) {
            System.out.println("");
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent
    }
like image 25
Alejandro Agapito Bautista Avatar answered Oct 26 '22 23:10

Alejandro Agapito Bautista


I would write like this:

public byte[] readFile(File pdfDoc) {
    if (!pdfDoc.exists()) {
        System.out.println("Could not find" + pdfDoc.getName() + " on the specified path");
        return null;
    }
    FileInputStream fin = null;
    byte fileContent[] = new byte[(int) pdfDoc.length()];

    try {
        fin = new FileInputStream(pdfDoc);
        fin.read(fileContent);
    } catch (FileNotFoundException e) {
        System.out.println("");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != fin) {
            fin.close();
        }
    }   
    return fileContent;
}
like image 34
Rajesh734 Avatar answered Oct 27 '22 00:10

Rajesh734