Advertisement
jaVer404

level09.lesson08.task02

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