//catch{} can only catch those exceptions throw from try{}
public class TestClass{
public static void main(String args[]){
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");//catch block throw NullPointerException.
}
// IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1(){
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}
2. You can apply a label to any code block or a block level statement (such as a for statement) but not to individual statement such as loop X : int i = 10;
loop : // 1
{
System.out.println("Loop Lable line");
break loop; // 2
}
3. Either throws or catch{} is required if there was exception
class TestClass{
public static void main(String args[]) throws Exception{
try{
m1();
System.out.println("A");
} //这里没有catch block 但是method throws Exception,所以没有compile error
finally{
System.out.println("B");
}
System.out.println("C");
}
public static void m1() throws Exception { throw new Exception(); }
}
No comments:
Post a Comment