Advertisement
Guest User

Java exception rethrowing

a guest
Feb 23rd, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package tk.lslayer.temp;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class CustomErrorDemo {
  7.    
  8.     public String errorThrower() throws IndexOutOfBoundsException, NullPointerException  {
  9. //        List<String> myCoolList = null;  
  10.         List<String> myCoolList = new ArrayList<String>();
  11. //        Select one of two lines, and uncomment any
  12.        
  13.         String string = myCoolList.get(100500); //This makes Exception, depends on selected line on top
  14.         return string;
  15.     }
  16.    
  17.     public void errorCatcher() throws MyCooleCustomException {
  18.         try {
  19.             this.errorThrower();  //Exception will be here
  20.         } catch (Exception e) {
  21.             //rethrowing, wrapped in our MyCoolecustomException
  22.             throw new MyCooleCustomException("We cought an exception!", e);
  23.         }
  24.     }
  25.    
  26.     public static void main(String args[]) {
  27.         CustomErrorDemo handler = new CustomErrorDemo();
  28.         try {
  29.             handler.errorCatcher();
  30.         } catch (MyCooleCustomException e) {
  31.             System.out.println(e.getMessage()); //We caught MyCooleCustomException
  32.             //warning, "Information expert" violated
  33.             System.out.println(e.getCause().getClass().toString()); //We show a class of caught exception
  34.         }
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement