在看版本代码的时候,发现有如下的代码,我们在平时的编码中可能也会遇到,很容易造成句柄没有关闭的问题:
finally
{
try
{
if (null != in)
{
in.close();
}
if (null != out)
{
out.close();
}
if (null != out)
{
socket.close();
}
}
catch (Throwable t1)
{
throw new TransException(t1);
}
}
1.在finally中关闭句柄时,一定要对各个句柄的关闭单独进行try操作,而不能对所有的关闭操作只进行一次try操作
2.不要在finally中随便进行抛异常操作,捕获到异常应该打印日志
finally
{
try
{
if (null != in)
{
in.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
try
{
if (null != out)
{
out.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
try
{
if (null != out)
{
socket.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
}
finally
{
try
{
if (null != in)
{
in.close();
}
if (null != out)
{
out.close();
}
if (null != out)
{
socket.close();
}
}
catch (Throwable t1)
{
throw new TransException(t1);
}
}
1.在finally中关闭句柄时,一定要对各个句柄的关闭单独进行try操作,而不能对所有的关闭操作只进行一次try操作
2.不要在finally中随便进行抛异常操作,捕获到异常应该打印日志
finally
{
try
{
if (null != in)
{
in.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
try
{
if (null != out)
{
out.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
try
{
if (null != out)
{
socket.close();
}
}
catch(Throwable t1)
{
System.out.println(t1.toString());
}
}