Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package excpt;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.lang.*;
- import java.lang.reflect.*;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.ConcurrentModificationException;
- import java.util.LinkedList;
- import java.util.List;
- /*
- * Наиболее полный список исключений
- * https://programming.guide/java/list-of-java-exceptions.html
- *
- * */
- class MyEx extends IOException {
- private int a = 1;
- public int b = 2;
- public MyEx() {
- }
- public MyEx(String str) {
- super(str);
- }
- public void UserEx() throws MyEx {
- throw new MyEx("demo");
- }
- }
- public class Excpt {
- static MyEx me = new MyEx();
- public static void main(String[] args) throws UnknownHostException, IOException {
- SockEx();
- ClssNotF();
- UserEx();
- IllegAcss();
- NoSuchmet();
- FileNotFoundExceptionExample();
- ClassCastExceptionExample();
- InterruptedExceptionExample();
- }
- public static void SockEx() throws UnknownHostException, IOException {
- // InetAddress host = InetAddress.getLocalHost();
- InetAddress host = null;
- Socket socket = null;
- try {
- socket = new Socket(host.getHostName(), 9876);
- } catch (NullPointerException e) {
- System.out.println("���������� 1 ����������!");
- System.out.println(e.toString());
- }
- }
- public static void ClssNotF() {
- try {
- Class.forName("MyHiddenClass");
- } catch (ClassNotFoundException e) {
- System.out.println("���������� 2 ����������!");
- System.out.println(e.toString());
- }
- }
- public static void UserEx() {
- try {
- me.UserEx();
- } catch (MyEx e) {
- System.out.println("���������� 3 ����������!");
- System.out.println(e.toString());
- }
- }
- public static void IllegAcss() {
- try {
- Field[] fields = me.getClass().getDeclaredFields();
- for (Field field : fields) {
- /*
- * if (Modifier.isPrivate(field.getModifiers())) { field.setAccessible(true);
- * System.out.println(field.getName()+" : "+field.get(me)); }
- */
- System.out.println(field.getName() + " : " + field.get(me));
- }
- } catch (IllegalAccessException e) {
- System.out.println("���������� 4 ����������!");
- System.out.println(e.toString());
- }
- }
- public static void NoSuchmet() {
- Method m = null;
- Class c = me.getClass();
- try {
- m = c.getMethod("jkjhg", null);
- System.out.println(m);
- } catch (NoSuchMethodException e) {
- System.out.println("���������� 5 ����������!");
- System.out.println(e.toString());
- }
- }
- public static void FileNotFoundExceptionExample() {
- try {
- var test = new FileReader("there_is_no_file.txt");
- /*such file does not exist*/
- } catch (FileNotFoundException e) {
- System.out.println("testing FileNotFoundException! #6");
- System.out.println(e.toString());
- }
- }
- public static void ClassCastExceptionExample() {
- try {
- List<String> list = new LinkedList<>();
- ArrayList<String> iDontGonnaWorkLol = (ArrayList<String>) list;
- } catch (ClassCastException e) {
- System.out.println("Broking class cast~ #7");
- System.out.println(e.toString());
- }
- }
- public static void InterruptedExceptionExample() {
- List<String> list = new LinkedList<>();
- list.add("a");
- list.add("b");
- list.add("c");
- try {
- for(String next: list)
- list.add("d");
- } catch(ConcurrentModificationException ex) {
- System.out.println("Concurrent Modification Exception testing #8");
- System.out.println(ex.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement