Advertisement
Haifisch7734

Exception

May 14th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package pl.laboratorium;
  2.  
  3. import java.util.Scanner;
  4. import java.util.concurrent.TimeUnit;
  5. import java.util.Random;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9.  
  10. interface Interfejs {
  11.     public void metoda(int x);
  12. }
  13.  
  14. public class Exc implements Interfejs {
  15.     int[] x = new int[5];
  16.  
  17.     public Exc() {
  18.         for (int i = 0; i < 5; i++)
  19.             x[i] = i * 2;
  20.     }
  21.  
  22.     int getLiczbe(int i) throws Exception {
  23.         return x[i];
  24.     }
  25.  
  26.     void pomocnik() {
  27.         System.out.println("Tablica ma tylko " + x.length + " elementow");
  28.     }
  29.  
  30.     @Override
  31.     public void metoda(int x) {
  32.         try {
  33.             System.out.println(this.getLiczbe(x));
  34.         } catch (Exception e) {
  35.             throw new RuntimeException(e);
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         Exc exc = new Exc();
  41.         Scanner in = new Scanner(System.in);
  42.         System.out.println("Podaj indeks elementu do odczytania");
  43.         int x = in.nextInt();
  44.  
  45.         // Sposob nr 1
  46.         try {
  47.             System.out.println(exc.getLiczbe(x));
  48.         } catch (Exception e) {
  49.             e.printStackTrace(System.out);
  50.         }
  51.  
  52.         System.out.println();
  53.        
  54.         // Sposob nr 2
  55.         try {
  56.             System.out.println(exc.getLiczbe(x));
  57.         } catch (Exception e) {
  58.             int answer = JOptionPane.showConfirmDialog(null,
  59.                     "Koniec programu?", "Podejmij decyzję",
  60.                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
  61.                     null);
  62.             if (answer == JOptionPane.YES_OPTION) {
  63.                 System.out.println("Koniec programu... Ale tylko w przenosni");
  64.             } else if (answer == JOptionPane.NO_OPTION) {
  65.                 System.out.println("Nie kończymy programu...");
  66.             }
  67.         }
  68.  
  69.         System.out.println();
  70.        
  71.         // Sposob nr 3
  72.         try {
  73.             System.out.println(exc.getLiczbe(x));
  74.         } catch (Exception e) {
  75.             System.out.println("Uzywam wartosci domyslnej");
  76.             System.out.println(exc.getLiczbe(exc.x.length - 1));
  77.         }
  78.  
  79.         System.out.println();
  80.        
  81.         // Sposob nr 4
  82.         try {
  83.             System.out.println(exc.getLiczbe(x));
  84.         } catch (Exception e) {
  85.             System.out.println("Wywołuję inna metode");
  86.             exc.pomocnik();
  87.         }
  88.  
  89.         System.out.println();
  90.        
  91.         // Sposob nr 5
  92.         // exc.metoda(x);
  93.  
  94.         System.out.println();
  95.        
  96.         // Sposob nr 6
  97.         try {
  98.             System.out.println(exc.getLiczbe(x));
  99.         } catch (Exception e) {
  100.             System.out.println("Ignoruję problem");
  101.         }
  102.  
  103.         System.out.println();
  104.        
  105.         // Sposob nr 7
  106.         for (int i = 0; i < 5; i++) {
  107.             try {
  108.                 System.out.println(exc.getLiczbe(x));
  109.                 break;
  110.             } catch (Exception e) {
  111.                 System.out.println("Wyjątek przechwycony [" + i + "]");
  112.                 e.printStackTrace(System.out);
  113.                 try {
  114.                     TimeUnit.SECONDS.sleep(2);
  115.                 } catch (InterruptedException e2) {
  116.                     }
  117.             }
  118.         }
  119.        
  120.         System.out.println();
  121.        
  122.         // Sposob nr 8
  123.         Random r = new Random();
  124.         System.out.println("Obchodzę problem. Nie czytam wartości z tablicy, tylko sobie losuję. Wylosowałem "+r.nextInt());
  125.  
  126.         System.out.println();
  127.        
  128.         //Sposob nr 9
  129.         try {
  130.             System.out.println(exc.getLiczbe(x));
  131.         } catch (Exception e) {
  132.             System.out.println("Wyskoczył wyjątek. Przygotuję do zamknięcia program i go zamknę");
  133.             in.close();
  134.             System.exit(0xDEADBEEF);
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement