Java – тест по исключениям

By | 19/12/2013

Что будет в результате выполнения следующих программ? Учтите, что возможны ошибки времени выполнения и компиляции.

1)

public class MyException {

public static void main(String[] args) {

try {

throw new MyException();

} catch (MyException e) {

System.out.println(“b”);

} catch (Exception e) {

System.out.println(“c”);

} finally {

System.out.println(“d”);

}

}

}

2)

public class ExceptionOfMine extends Exception {

public static void main(String[] args) {

try {

throw new ExceptionOfMine();

} finally {

System.out.println(“c”);

}

}

}

3)

public class Exception3 {

public static void main(String[] args) throws Exception {

try {

System.out.println(‘a’);

throw new NullPointerException();

} catch (NullPointerException e) {

System.out.println(‘b’);

throw new Exception();

} catch (Exception e) {

System.out.println(‘c’);

}

}

}

 

4)

public class ExceptionOfMine extends Exception {

public static void main(String[] args) {

System.out.print(“hi!, “);

try {

System.out.print(“a”);

throw new ExceptionOfMine();

} catch (ExceptionOfMine e) {

System.out.print(“b”);

} catch (Exception e) {

System.out.print(“c”);

} finally {

System.out.print(“d”);

}

System.out.print(“e”);

}

}

 

5)

public class Exception4 {

public static void main(String[] args) {

System.out.println(“a”);

try {

System.out.println(“b”);

} catch (Exception e) {

System.out.println(“c”);

} finally {

System.out.println(“d”);

}

System.out.println(“e”);

}

}

6)

public class Exception5 {

public static int divide(int a, int b) {

try {

return a / b;

} catch (Exception e) {

return 0;

} finally {

return 10;

}

}

public static void main(String[] args) {

int i = divide(11, 1);

System.out.println(i);

}

}

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *