Whenever I use try-catch blocks in my code, my code always follows a pattern. First a try-catch block to open a resource followed by a null check and finally a try-catch block to read the resources. Is this a messy pattern? If so, what should the good design look like?
Here is an example of what I mean
public static void main(String[] args) {
Process process = null;
try {
process = Runtime.getRuntime().exec("C:\\program.exe");
} catch (IOException e) {
e.printStackTrace();
}
if (process == null) {
return;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}