指定默认值
问题描述:
URL test = null;
String inputLine = null;
BufferedReader in = null;
try {
test = new URL("http://localhost/out.php"); // if connection down
} catch (MalformedURLException e) {
inputLine = "test_synntax";
}
try {
in = new BufferedReader(new InputStreamReader(test.openStream()));
...
如何,如果默认的URL解析失败 在好日子指定默认值
答
您需要调用URL.openConnection
第一,然后把你的默认值在IOException
块:
try {
test = new URL("http://localhost/out.php");
URLConnection urlConn = test.openConnection();
in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
} catch (MalformedURLException e) {
inputLine = "test_synntax";
} catch (IOException e) {
inputLine = "test_synntax";
}
答
try {
test = new URL("http://localhost/out.php");
//I believe you need this and it throws IOException on timeout.
//Though it's still unclear to me what you mean by 'resolution' and 'disponible wifi/3g`?
test.openConnection();
} catch (MalformedURLException | IOException e) {
inputLine = "test_synntax";
}
这个例子语境中不应WIFI/3G连接disponible,谢谢你们指定的inputline的价值是java的7多重捕获语法,请介意。
答
-如果的WiFi/3G失败,则它会给UnknownHostException
。
所以,你可以处理他们这样....
try {
test = new URL("http://localhost/out.php");
} catch (UnknownHostException e) {
inputLine = "Connection error";
}
问题你不清楚你要做什么吗? –
并请形成适当的问题标题。 –
你在做什么样的工作呢? – PermGenError