我想從傳入的值(使用Bundle)在Android中創建一個IP地址,如果失敗,我使用硬編碼的默認IP地址創建它。如果失敗,我退出應用程序。在Java中嵌套try/catch語句是否可接受?
我想知道的是,如果它可以像我在這裏做的那樣嵌套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();
}
}
我會寫完全像這樣,包括評論 – 2011-04-25 02:00:44