[英]Is it acceptable practice in Java to nest try/catch statements?
我試圖從傳入的值(使用Bundle)在Android中創建一個IP地址,如果失敗,我將使用硬編碼的默認IP地址創建它。 如果失敗,那么我退出應用程序。
我想知道的是,如果可以像我在這里做的那樣嵌套try / catch,或者是否有更好的方法。
try {
// serverSettings is the Bundle name that was passed in.
ipAddress = InetAddress.getByName(serverSettings.getString("serverIp"));
} catch (UnknownHostException e) {
Log.e("ERROR:", "IOException: Failed to create IP, trying default");
try {
// DEFAULT_IP is the hard-coded default fall-back address
ipAddress = InetAddress.getByName(DEFAULT_IP);
} catch (UnknownHostException e1) {
Log.e("ERROR:", "IOException: Total fail, exiting");
e1.printStackTrace();
finish();
}
}
這是合法的Java。 它看起來很笨我,我可能會采用不同的方式,但它有效並且有效。
這是我如何做到的:
public InetAddress getServerAddress() {
for (String address : new String[] {serverSettings.getString("serverIp"), DEFAULT_IP}) {
try {
return InetAddress.getByName(address);
} catch (UnknownHostException e) {
Log.e("ERROR:", "Cannot resolve " + address);
}
}
Log.e("ERROR:", "Total fail, exiting");
finish();
return null; // not reached
}
我認為這將是有爭議的更好的方式,但這是另一種選擇,有些人可能認為更具“表現力”和可讀性,雖然它是更多的代碼行:
public InetAddress tryHost(String hostName) {
InetAddress address = null;
try {
address = InetAddress.getByName(hostName);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
然后在你的代碼中,只需:
InetAddress address = null;
address = tryHost(serverSettings.getString("serverIp"));
if (address = null)
address = tryHost(DEFAULT_IP);
if (address = null) {
// handle error, throw exception
}
finish();
另一種變體是首先設置默認值:
ipAddress = null;
try {
// serverSettings is the Bundle name that was passed in.
ipAddress = InetAddress.getByName(DEFAULT_IP); // Set default address
ipAddress = InetAddress.getByName(serverSettings.getString("serverIp")); // Try passed-in address
} catch (UnknownHostException e) {
if (ipAddress == null) {
Log.e("ERROR:", "IOException: Total fail, exiting");
e1.printStackTrace();
finish();
}
}
如果使用Bundle'd值的調用失敗,則在修改ipAddress之前拋出異常,因此ipAddress已設置為默認值。 當然,如果DEFAULT_IP始終可以解析,這只是一個有效的模式。
沒關系。 您還可以使用在第一個catch中打開的布爾標志,因此如果您的布爾標志打開,則在catch外部通過IP執行請求。
boolean failed = false;
try {
// serverSettings is the Bundle name that was passed in.
ipAddress = InetAddress.getByName(serverSettings.getString("serverIp"));
} catch (UnknownHostException e) {
failed = true;
Log.e("ERROR:", "IOException: Failed to create IP, trying default");
}
if(failed){
try {
// DEFAULT_IP is the hard-coded default fall-back address
ipAddress = InetAddress.getByName(DEFAULT_IP);
} catch (UnknownHostException e1) {
Log.e("ERROR:", "IOException: Total fail, exiting");
e1.printStackTrace();
finish();
}
}
}
我更喜歡這個。 它有點清潔,不涉及額外的標志。
InetAddress ipAddress = null;
try {
// serverSettings is the Bundle name that was passed in.
ipAddress = InetAddress.getByName(serverSettings.getString("serverIp"));
} catch (UnknownHostException e) {
Log.e("ERROR:", "IOException: Failed to create IP, trying default");
}
if(ipAddress==null){
try {
// DEFAULT_IP is the hard-coded default fall-back address
ipAddress = InetAddress.getByName(DEFAULT_IP);
} catch (UnknownHostException e1) {
Log.e("ERROR:", "IOException: Total fail, exiting");
e1.printStackTrace();
finish();
}
}
在進行更大規模的測試時,我喜歡另一種變化(在第一次勝利的基礎上):
ipAddress = null;
// serverSettings is the Bundle name that was passed in.
String[] addresses = { DEFAULT_IP,
serverSettings.getString("serverIp") };
int i = 0;
do {
try {
ipAddress = InetAddress.getByName(addresses[i]);
} catch (UnknownHostException e) {
Log.e("ERROR:", "IOException: Failed to create IP, trying next");
}
} while (ipAddress == null && i < addresses.length);
if (ipAddress == null) {
Log.e("ERROR:", "IOException: Total fail, exiting");
e1.printStackTrace();
finish();
}
這在我的正常用例中可能更合適(循環使用SimpleDateFormats來匹配第三方日期字符串)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.