๐ย ์ค๋ ์ฝ์ ๋ฒ์ : 7. ์ค๋ฅ์ฒ๋ฆฌ (130~137p)
public class DeviceController {
...
public void sendShutDown() {
try {
tryToShutDown();
} catch (DeviceShutDownError e) {
logger.log(e);
}
}
private void tryToShutDown() throws DeviceShutDownError {
DeviceHandle handle = getHandle(DEV1);
DeviceRecord record = retrieveDeviceRecord(handle);
pauseDevice(handle);
clearDeviceWorkQueue(handle);
closeDevice(handle);
}
private DeviceHandle getHandle(DeviceID id) { ...
throw new DeviceShutDownError("Invalid handle for: " + id.toString());
...
}
...
}
@Test(expected = StorageException.class)
public void retrieveSectionShouldThrowOnInvalidFileName() {
sectionStore.retrieveSection("invalid - file");
}
public List<RecordedGrip> retrieveSection(String sectionName) {
// ์ค์ ๋ก ๊ตฌํํ ๋๊น์ง ๋น์ด ์๋ ๋๋ฏธ๋ฅผ ๋ฐํํ๋ค.
return new ArrayList<RecordedGrip>();
}
์์ธ๋ฅผ ๋์ง์ง ์์ผ๋ฏ๋ก ๋จ์ ํ
์คํธ ์คํจ โ ์๋ชป๋ ํ์ผ ์ ๊ทผ์ผ๋ก ๋ณ๊ฒฝpublic List < RecordedGrip > retrieveSection(String sectionName) {
try {
FileInputStream stream = new FileInputStream(sectionName)
} catch (Exception e) {
throw new StorageException("retrieval error", e);
}
return new ArrayList < RecordedGrip > ();
}
Refactoring: catch ์์ธ ์ ํ ๋ฒ์๋ฅผ ์ขํ๋ค Exception โ FileNotFoundExceptionpublic List < RecordedGrip > retrieveSection(String sectionName) {
try {
FileInputStream stream = new FileInputStream(sectionName);
stream.close();
} catch (FileNotFoundException e) {
throw new StorageException("retrieval error", e);
}
return new ArrayList < RecordedGrip > ();
}
๋๋จธ์ง ํ์ํ ๋
ผ๋ฆฌ๋ฅผ TDD๋ก ์ถ๊ฐํ๋คLocalPort port = new LocalPort(12);
try {
port.open();
} catch (PortDeviceFailure e) {
reportError(e);
logger.log(e.getMessage(), e);
} finally {
...
}
public class LocalPort {
private ACMEPort innerPort;
public LocalPort(int portNumber) {
innerPort = new ACMEPort(portNumber);
}
public void open() {
try {
innerPort.open();
} catch (DeviceResponseException e) {
throw new PortDeviceFailure(e);
} catch (ATM1212UnlockedException e) {
throw new PortDeviceFailure(e);
} catch (GMXError e) {
throw new PortDeviceFailure(e);
}
}
...
}