總結Java會出現"unreachable code"錯誤的幾個例子
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");
- }
- }
- }