读书人

java之try与finally话语(2)

发布时间: 2012-12-21 12:03:49 作者: rapoo

java之try与finally语句(2)
接上一篇,跟上一篇代码差不多,就是修改了a的值为double类型。

整 型 不可以除以0,但double可以除以0



Java代码
public class TryTest
{
public static void main(String[] args)
{
TryTest tt = new TryTest();

tt.test1();

System.out.println();

tt.test2();

System.out.println();

tt.test3();
}

public void test1()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

}finally
{
System.out.println("finally");
}

System.out.println("end");
}

public void test2()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

return;

}finally
{
System.out.println("finally");
}

System.out.println("end");
}

public void test3()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

System.exit(0);

}finally
{
System.out.println("finally");
}

System.out.println("end");
}
}

public class TryTest
{
public static void main(String[] args)
{
TryTest tt = new TryTest();

tt.test1();

System.out.println();

tt.test2();

System.out.println();

tt.test3();
}

public void test1()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

}finally
{
System.out.println("finally");
}

System.out.println("end");
}

public void test2()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

return;

}finally
{
System.out.println("finally");
}

System.out.println("end");
}

public void test3()
{
double a = 2.0;

System.out.println("start");

try
{
a = a/0;

}catch(Exception e)
{
System.out.println("catch");

System.exit(0);

}finally
{
System.out.println("finally");
}

System.out.println("end");
}
}

输出为:



Java代码
start
finally
end

start
finally
end

start
finally
end

读书人网 >编程

热点推荐