public class exam {
static int num=5;
static int m1(){
try{
num=6;
throw new NullPointerException();
System.out.println("111"); //编译不通过
}catch(ArrayIndexOutOfBoundsException e){
num=10;
System.out.println("222");
}catch(Exception e){
num+=1;
System.out.println("333");
return num;
}finally{
num=10;
System.out.println("444");
throw new NullPointerException();
}
} public static void main(String[] args) {
try{
m1();
num=20;
}catch(Exception e){
num+=1;
System.out.println("555");
}
System.out.println(exam.num); }
} 输出结果:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at exam.m1(exam.java:8)
at exam.main(exam.java:25)
1. throw关键字
Java代码:
- public void XXX() throws Throwable{
- throw new Throwable();
- System.out.println("test");
- }
2. return关键字
Java代码 :
- public void XXX() {
- return;
- System.out.println("test");
- }
3. continue关键字
Java代码:
- public void XXX() {
- for(int i=0; i<10; i++) {
- continue;
- System.out.println("test");
- }
- }
4. break关键字
Java代码:
- public void XXX() {
- for(int i=0; i<10; i++) {
- if(i==5) {
- break;
- System.out.println("test");
- }
- }
- }
5. while(true):
Java代码:
- public static void XXX() {
- while(true);
- System.out.println("when?");
- }