idzik

Java-BootCamp-Exceptions-MojWyjatek

Nov 15th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public class MojWyjatek extends Exception {
  2. public MojWyjatek(){
  3.     super();
  4. }
  5. public MojWyjatek(String przyczyna){
  6.     super();
  7.     System.out.println("Wystapił wyjątek a przyczyna to "+przyczyna);
  8. }
  9. }
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public class Gra{
  16.     public int dodaj(int a,int b) throws MojWyjatek{
  17.         if (a+b>10) throw new MojWyjatek("za dużo");
  18.         return a+b;
  19.     }
  20.     public static void main(String [] args) {
  21.     int a=10;
  22.     int b=12;
  23.     Gra game=new Gra();
  24.     try {
  25.         game.dodaj(a,b);
  26.     } catch (MojWyjatek e) {
  27.         // TODO Auto-generated catch block
  28.         e.printStackTrace();
  29.     }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment