Advertisement
teaowl

Excpt.java

Oct 16th, 2021
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1.  
  2. package excpt;
  3.  
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.lang.*;
  8. import java.lang.reflect.*;
  9. import java.net.*;
  10. import java.util.ArrayList;
  11. import java.util.ConcurrentModificationException;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14.  
  15. /*
  16.  * Наиболее полный список исключений
  17.  * https://programming.guide/java/list-of-java-exceptions.html
  18.  *
  19.  * */
  20.  
  21.  
  22. class MyEx extends IOException {
  23.     private int a = 1;
  24.     public int b = 2;
  25.  
  26.     public MyEx() {
  27.     }
  28.  
  29.     public MyEx(String str) {
  30.         super(str);
  31.     }
  32.  
  33.     public void UserEx() throws MyEx {
  34.         throw new MyEx("demo");
  35.     }
  36.  
  37. }
  38.  
  39. public class Excpt {
  40.  
  41.     static MyEx me = new MyEx();
  42.  
  43.     public static void main(String[] args) throws UnknownHostException, IOException {
  44.  
  45.         SockEx();
  46.         ClssNotF();
  47.         UserEx();
  48.         IllegAcss();
  49.         NoSuchmet();
  50.         FileNotFoundExceptionExample();
  51.         ClassCastExceptionExample();
  52.         InterruptedExceptionExample();
  53.     }
  54.  
  55.     public static void SockEx() throws UnknownHostException, IOException {
  56.         // InetAddress host = InetAddress.getLocalHost();
  57.         InetAddress host = null;
  58.         Socket socket = null;
  59.  
  60.         try {
  61.             socket = new Socket(host.getHostName(), 9876);
  62.  
  63.         } catch (NullPointerException e) {
  64.             System.out.println("���������� 1 ����������!");
  65.             System.out.println(e.toString());
  66.         }
  67.  
  68.     }
  69.  
  70.     public static void ClssNotF() {
  71.  
  72.         try {
  73.             Class.forName("MyHiddenClass");
  74.  
  75.         } catch (ClassNotFoundException e) {
  76.             System.out.println("���������� 2 ����������!");
  77.             System.out.println(e.toString());
  78.         }
  79.     }
  80.  
  81.     public static void UserEx() {
  82.  
  83.         try {
  84.  
  85.             me.UserEx();
  86.  
  87.         } catch (MyEx e) {
  88.             System.out.println("���������� 3 ����������!");
  89.             System.out.println(e.toString());
  90.         }
  91.     }
  92.  
  93.     public static void IllegAcss() {
  94.  
  95.         try {
  96.  
  97.             Field[] fields = me.getClass().getDeclaredFields();
  98.             for (Field field : fields) {
  99.                 /*
  100.                  * if (Modifier.isPrivate(field.getModifiers())) { field.setAccessible(true);
  101.                  * System.out.println(field.getName()+" : "+field.get(me)); }
  102.                  */
  103.                 System.out.println(field.getName() + " : " + field.get(me));
  104.             }
  105.  
  106.         } catch (IllegalAccessException e) {
  107.  
  108.             System.out.println("���������� 4 ����������!");
  109.             System.out.println(e.toString());
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.     public static void NoSuchmet() {
  116.  
  117.         Method m = null;
  118.         Class c = me.getClass();
  119.  
  120.         try {
  121.             m = c.getMethod("jkjhg", null);
  122.  
  123.             System.out.println(m);
  124.  
  125.         } catch (NoSuchMethodException e) {
  126.             System.out.println("���������� 5 ����������!");
  127.             System.out.println(e.toString());
  128.         }
  129.     }
  130.    
  131.     public static void FileNotFoundExceptionExample() {
  132.         try {
  133.             var test = new FileReader("there_is_no_file.txt");
  134.             /*such file does not exist*/
  135.         } catch (FileNotFoundException e) {
  136.             System.out.println("testing FileNotFoundException! #6");
  137.             System.out.println(e.toString());
  138.         }
  139.     }
  140.    
  141.     public static void ClassCastExceptionExample() {
  142.         try {
  143.             List<String> list = new LinkedList<>();
  144.             ArrayList<String> iDontGonnaWorkLol = (ArrayList<String>) list;
  145.         } catch (ClassCastException e) {
  146.             System.out.println("Broking class cast~ #7");
  147.             System.out.println(e.toString());
  148.         }
  149.     }
  150.    
  151.     public static void InterruptedExceptionExample() {
  152.         List<String> list = new LinkedList<>();
  153.         list.add("a");
  154.         list.add("b");
  155.         list.add("c");
  156.         try {
  157.         for(String next: list)  
  158.             list.add("d");
  159.         } catch(ConcurrentModificationException ex) {
  160.             System.out.println("Concurrent Modification Exception testing #8");
  161.             System.out.println(ex.toString());
  162.         }
  163.     }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement