Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.laboratorium;
- import java.util.Scanner;
- import java.util.concurrent.TimeUnit;
- import java.util.Random;
- import javax.swing.JOptionPane;
- interface Interfejs {
- public void metoda(int x);
- }
- public class Exc implements Interfejs {
- int[] x = new int[5];
- public Exc() {
- for (int i = 0; i < 5; i++)
- x[i] = i * 2;
- }
- int getLiczbe(int i) throws Exception {
- return x[i];
- }
- void pomocnik() {
- System.out.println("Tablica ma tylko " + x.length + " elementow");
- }
- @Override
- public void metoda(int x) {
- try {
- System.out.println(this.getLiczbe(x));
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public static void main(String[] args) throws Exception {
- Exc exc = new Exc();
- Scanner in = new Scanner(System.in);
- System.out.println("Podaj indeks elementu do odczytania");
- int x = in.nextInt();
- // Sposob nr 1
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- e.printStackTrace(System.out);
- }
- System.out.println();
- // Sposob nr 2
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- int answer = JOptionPane.showConfirmDialog(null,
- "Koniec programu?", "Podejmij decyzję",
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
- null);
- if (answer == JOptionPane.YES_OPTION) {
- System.out.println("Koniec programu... Ale tylko w przenosni");
- } else if (answer == JOptionPane.NO_OPTION) {
- System.out.println("Nie kończymy programu...");
- }
- }
- System.out.println();
- // Sposob nr 3
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- System.out.println("Uzywam wartosci domyslnej");
- System.out.println(exc.getLiczbe(exc.x.length - 1));
- }
- System.out.println();
- // Sposob nr 4
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- System.out.println("Wywołuję inna metode");
- exc.pomocnik();
- }
- System.out.println();
- // Sposob nr 5
- // exc.metoda(x);
- System.out.println();
- // Sposob nr 6
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- System.out.println("Ignoruję problem");
- }
- System.out.println();
- // Sposob nr 7
- for (int i = 0; i < 5; i++) {
- try {
- System.out.println(exc.getLiczbe(x));
- break;
- } catch (Exception e) {
- System.out.println("Wyjątek przechwycony [" + i + "]");
- e.printStackTrace(System.out);
- try {
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e2) {
- }
- }
- }
- System.out.println();
- // Sposob nr 8
- Random r = new Random();
- System.out.println("Obchodzę problem. Nie czytam wartości z tablicy, tylko sobie losuję. Wylosowałem "+r.nextInt());
- System.out.println();
- //Sposob nr 9
- try {
- System.out.println(exc.getLiczbe(x));
- } catch (Exception e) {
- System.out.println("Wyskoczył wyjątek. Przygotuję do zamknięcia program i go zamknę");
- in.close();
- System.exit(0xDEADBEEF);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement