直接上代码和运行结果,等到以后自己看不懂的时候再来注释
1 程序
public static void main(String[] args) {
int a[]={0,1,2};
try {
a[4]=8;
int i=1/0;
method1(1);
} catch (IOException e) {
System.out.println("IOException");
} catch (ArithmeticException e){
System.out.println("ArithmeticException");
} catch (Exception e){
System.out.println("Exception");
}finally{
System.out.println("Finally");
}
};
其中:
public static void method1(int i) throws IOException{
throw new IOException();
}
结果
Exception
Finally
2 程序
public static void main(String[] args) {<span> </span>
<span> </span>int a[]={0,1,2};
<span> </span>try {
<span> </span>
<span> </span>int i=1/0;
<span> </span>method1(1);
<span> </span>a[4]=8;
<span> </span>
<span> </span>} catch (IOException e) {
<span> </span>System.out.println("IOException");
<span> </span>} catch (ArithmeticException e){
<span> </span>System.out.println("ArithmeticException");
<span> </span>} catch (Exception e){
<span> </span>System.out.println("Exception");
<span> </span>}finally{
<span> </span>System.out.println("Finally");
<span> </span>}
<span> </span>
<span> </span>};
结果
ArithmeticException
Finally
3 程序
public static void main(String[] args) {<span> </span>
<span> </span>int a[]={0,1,2};
<span> </span>try {
<span> </span>
<span> </span>
<span> </span>method1(1);
<span> </span>int i=1/0;
<span> </span>a[4]=8;
<span> </span>
<span> </span>} catch (IOException e) {
<span> </span>System.out.println("IOException");
<span> </span>} catch (ArithmeticException e){
<span> </span>System.out.println("ArithmeticException");
<span> </span>} catch (Exception e){
<span> </span>System.out.println("Exception");
<span> </span>}finally{
<span> </span>System.out.println("Finally");
<span> </span>}
<span> </span>
<span> </span>};
结果
IOException
Finally
4 程序
<span style="white-space:pre"> </span>public static void main(String[] args) {<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>int a[]={0,1,2};
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>method1(1);
<span style="white-space:pre"> </span>int i=1/0;
<span style="white-space:pre"> </span>a[4]=8;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>} catch (Exception e) {
<span style="white-space:pre"> </span>System.out.println("Exception");
<span style="white-space:pre"> </span>} catch (ArithmeticException e){
<span style="white-space:pre"> </span>System.out.println("ArithmeticException");
<span style="white-space:pre"> </span>} catch (IOException e){
<span style="white-space:pre"> </span>System.out.println("IOException");
<span style="white-space:pre"> </span>}finally{
<span style="white-space:pre"> </span>System.out.println("Finally");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>};
结果:报错
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
<span style="white-space:pre"> </span>Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception
<span style="white-space:pre"> </span>Unreachable catch block for IOException. It is already handled by the catch block for Exception