class

Exceptions in Constructors

In this example we shall show you how to handle exceptions in constructors. To handle exceptions in constructors we have performed the following steps:

  • We have created a class, InputFile, that has a BufferedReader field.
  • In its constructor, it gets a String and it creates a new FileReader with the given String name of path to file to read from. A FileNotFoundException might be thrown here, that needs to be caught, but since the file is not found the BufferedReader is not opened so does not need to close.
  • If any other exception occurs after that, the FileReader has opened so it has to close.
  • The InputFile class also has two methods, getLine(), that gets the line of the text in the BufferedReader, and dispose() that closes the BufferedReader. The methods throw RuntimeException that has to be caught.
  • We create a new instance of InputFile, with a given String. Since the constructor of the class is called if the file is not found the exception will be thrown,

as described in the code snippet below.  

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.javacodegeeks.snippets.core;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
 
public class ExceptionInConstructor {
 
    public static void main(String[] args) {
 
   
 
  try {
 
 
InputFile inputFile = new InputFile("Cleanup.java");
 
 
String string;
 
 
int i = 1;
 
 
while ((string = inputFile.getLine()) != null)
 
  ; // Perform line-by-line processing here...
 
 
inputFile.dispose();
 
  } catch (Exception e) {
 
 
System.err.println("Caught Exception in main");
 
 
e.printStackTrace();
 
  }
    }
}
 
class InputFile {
 
    private BufferedReader input;
 
    public InputFile(String fileName) throws Exception {
 
  try {
 
 
input = new BufferedReader(new FileReader(fileName));
 
 
// Other code that might throw exceptions
 
  } catch (FileNotFoundException e) {
 
 
System.err.println("Could not open " + fileName);
 
 
// Wasn't open, so don't close it
 
 
throw e;
 
  } catch (Exception e) {
 
 
// All other exceptions must close it
 
 
try {
 
 
    input.close();
 
 
} catch (IOException e2) {
 
 
    System.err.println("in.close() unsuccessful");
 
 
}
 
 
throw e; // Rethrow
 
  } finally {
 
 
// Don't close it here!!!
 
  }
    }
 
    public String getLine() {
 
  String s;
 
  try {
 
 
s = input.readLine();
 
  } catch (IOException e) {
 
 
throw new RuntimeException("readLine() failed");
 
  }
 
  return s;
    }
 
    public void dispose() {
 
  try {
 
 
input.close();
 
 
System.out.println("dispose() successful");
 
  } catch (IOException e2) {
 
 
throw new RuntimeException("in.close() failed");
 
  }
    }
}

Output:

dispose() successful

  
This was an example of how to handle exceptions in constructors in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button