【发布时间】:2014-11-09 16:30:44
【问题描述】:
我正在开发一个应用程序,该应用程序通过套接字连接连接到服务器以读取和写入数据。 我在 AsyncTask 中使用 runInBackground() 方法来执行此操作。
我将我的 PC 用作服务器并将我的 android 设备连接到 WiFi 以连接到 PC。
我想模拟服务器不可用的情况,因此我断开设备与 WiFi 的连接并运行代码。
我使用以下代码:
try
{
socket = new Socket();
socket.connect(new InetSocketAddress("192.168.1.104", 4444), 10000);
...
...
}
catch (SocketTimeoutException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
在 10 秒后按预期抛出 SocketTimeoutException,但问题是没有一个 catch 块实际上捕获异常。这怎么可能?
这是日志:
11-09 17:57:15.286: W/System.err(12050): java.net.SocketTimeoutException: failed to connect to /192.168.1.104 (port 4444) after 10000ms
11-09 17:57:15.301: W/System.err(12050): at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
11-09 17:57:15.301: W/System.err(12050): at libcore.io.IoBridge.connect(IoBridge.java:112)
11-09 17:57:15.301: W/System.err(12050): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-09 17:57:15.301: W/System.err(12050): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-09 17:57:15.306: W/System.err(12050): at java.net.Socket.connect(Socket.java:842)
11-09 17:57:15.306: W/System.err(12050): at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:183)
11-09 17:57:15.306: W/System.err(12050): at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:1)
11-09 17:57:15.311: W/System.err(12050): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-09 17:57:15.311: W/System.err(12050): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-09 17:57:15.311: W/System.err(12050): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-09 17:57:15.311: W/System.err(12050): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-09 17:57:15.316: W/System.err(12050): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-09 17:57:15.316: W/System.err(12050): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-09 17:57:15.316: W/System.err(12050): at java.lang.Thread.run(Thread.java:856)
11-09 17:57:15.316: I/System.out(12050): closed connection to server
另外,我查看了 Socket.connect(InetSocketAddress, timeout) 方法的文档,并没有说它曾经抛出过 SocketTimeoutException 异常,因此更加混乱。
Parameters
remoteAddr the address and port of the remote host to connect to.
timeout the timeout value in milliseconds or 0 for an infinite timeout.
Throws
IllegalArgumentException if the given SocketAddress is invalid or not supported or the timeout value is negative.
IOException if the socket is already connected or an error occurs while connecting.
顺便说一句,我的应用程序没有崩溃。我是否可以假设这是因为它在不同的线程中?
【问题讨论】:
-
你正在捕捉它,然后打印堆栈跟踪......
-
是的,我没有意识到,谢谢。对了,你能解释一下为什么当文档没有提到抛出那种异常的可能性时它会抛出 SocketTimeoutException 吗?
-
并非所有异常都需要明确抛出。例如 RuntimeException
-
或者,更好的是,SocketTimeoutException 属于 IOException,我很确定您的班级至少会抛出这个(包括 STE)