ya_zay4eg

level09.lesson08.task02

Jan 13th, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package com.javarush.test.level09.lesson08.task02;
  2.  
  3. /* Перехватывание исключений
  4. 1. Есть три исключения последовательно унаследованные от Exception:
  5. 2. class Exception1 extends  Exception
  6. 3. class Exception2 extends  Exception1
  7. 4. class Exception3 extends  Exception2
  8.  
  9. Exception-->Exception1-->Exception2-->Exception3
  10. 5. Есть метод, который описан так:
  11. 5.1. public static void method1() throws Exception1, Exception2, Exception3
  12.  
  13. 6. Напиши catch, который перехватит все три Exception1, Exception2 и Exception3
  14. */
  15.  
  16. public class Solution
  17. {
  18.     public static void main(String[] args) throws Exception
  19.     {
  20.       //Напишите тут ваш код
  21.         try
  22.         {
  23.             method1();
  24.         }
  25.  
  26.  
  27.  
  28.       //Напишите тут ваш код
  29.         catch (Exception e) {
  30.  
  31.         }
  32.  
  33.     }
  34.  
  35.     public static void method1() throws Exception1, Exception2, Exception3
  36.     {
  37.         int i = (int) (Math.random() * 3);
  38.         if (i == 0)
  39.             throw new Exception1();
  40.         if (i == 1)
  41.             throw new Exception2();
  42.         if (i == 2)
  43.             throw new Exception3();
  44.     }
  45. }
  46.  
  47. class Exception1 extends Exception {
  48. }
  49.  
  50. class Exception2 extends Exception1 {
  51. }
  52.  
  53. class Exception3 extends Exception2 {
  54. }
Advertisement
Add Comment
Please, Sign In to add comment