Java Try Catch block

前端 未结 2 545
庸人自扰
庸人自扰 2021-01-21 03:29

I initially started programming in college and learnt vb.net. Now I have decided to make the move to Java and have some queries. In vb, the try catch statement is laid out as fo

2条回答
  •  旧巷少年郎
    2021-01-21 04:11

    The code that is on the page that is in link i have modified it with single exception. Problem here is that in this case you will not able to know that exception is where whether due to

    IndexOutOfBoundsException or IOException

    just you know that a exception occurs

    import java.io.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListOfNumbers {
    
        public static void main(String... s) {
            ListOfNumbers lon = new ListOfNumbers();
            lon.writeList();
        }
    
        private List list;
        private static final int SIZE = 10;
    
        public ListOfNumbers() {
            list = new ArrayList(SIZE);
            for (int i = 0; i < SIZE; i++) {
                list.add(new Integer(i));
            }
        }
    
        public void writeList() {
            PrintWriter out = null;
    
            try {
                System.out.println("Entering" + " try statement");
    
                out = new PrintWriter(new FileWriter("e://OutFile.txt"));
                for (int i = 0; i < SIZE; i++) {
                    out.println("Value at: " + i + " = " + list.get(i));
                }
            } catch (Exception e) {
                System.err.println("Caught Exception: " + e.getMessage());
    
            } finally {
                if (out != null) {
                    System.out.println("Closing PrintWriter");
                    out.close();
                } else {
                    System.out.println("PrintWriter not open");
                }
            }
        }
    }
    

    Let us understand the concept it is better to know why the code fails due to which particular type of exception whether

    IndexOutOfBoundsException or IOException

    Now The Code with handling of different Exception

    import java.io.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class ListOfNumbers {
    
        public static void main(String... s) {
            ListOfNumbers lon = new ListOfNumbers();
            lon.writeList();
        }
    
        private List list;
        private static final int SIZE = 10;
    
        public ListOfNumbers() {
            list = new ArrayList(SIZE);
            for (int i = 0; i < SIZE; i++) {
                list.add(new Integer(i));
            }
        }
    
        public void writeList() {
            PrintWriter out = null;
    
            try {
                System.out.println("Entering" + " try statement");
    
                out = new PrintWriter(new FileWriter("e://OutFile.txt"));
                for (int i = 0; i < SIZE; i++) {
                    out.println("Value at: " + i + " = " + list.get(i));
                }
            } catch (IndexOutOfBoundsException e) {
                System.err.println("Caught IndexOutOfBoundsException: " +
                                   e.getMessage());
    
            } catch (IOException e) {
                System.err.println("Caught IOException: " + e.getMessage());
    
            } finally {
                if (out != null) {
                    System.out.println("Closing PrintWriter");
                    out.close();
                } else {
                    System.out.println("PrintWriter not open");
                }
            }
        }
    }
    

    Here we could come to know that whether it fails due to creation of file at location

    e://OutFile.txt drive Not on my system

    error printed as

    Caught Exception: e:\OutFile.txt (The system cannot find the path specified) Entering try statement PrintWriter not open

    Next Case

    Now when i comment the line

    list.add(new Integer(i));

    import java.io.*;
    import java.util.List;
    import java.util.ArrayList;
    
        public class ListOfNumbers {
    
            public static void main(String... s) {
                ListOfNumbers lon = new ListOfNumbers();
                lon.writeList();
            }
    
            private List list;
            private static final int SIZE = 10;
    
            public ListOfNumbers() {
                list = new ArrayList(SIZE);
                for (int i = 0; i < SIZE; i++) {
                    //    list.add(new Integer(i));
                }
            }
    
            public void writeList() {
                PrintWriter out = null;
    
                try {
                    System.out.println("Entering" + " try statement");
    
                    out = new PrintWriter(new FileWriter("OutFile.txt"));
                    for (int i = 0; i < SIZE; i++) {
                        out.println("Value at: " + i + " = " + list.get(i));
                    }
                } catch (IndexOutOfBoundsException e) {
                    System.err.println("Caught IndexOutOfBoundsException: " +
                                       e.getMessage());
    
                } catch (IOException e) {
                    System.err.println("Caught IOException: " + e.getMessage());
    
                } finally {
                    if (out != null) {
                        System.out.println("Closing PrintWriter");
                        out.close();
                    } else {
                        System.out.println("PrintWriter not open");
                    }
                }
            }
        }
    

    then it clearly says that it fails for index out of bound exception

    Entering try statement Caught IndexOutOfBoundsException: Index: 0, Size: 0 Closing PrintWriter

    So for the purpose of debugging the application properly and efficiently it is good.

    I have created condition for the other type of exception

    NoClassDefFoundError
    
    java.lang.NoClassDefFoundError: ListOfNumbers
    Caused by: java.lang.ClassNotFoundException: stackprac.ListOfNumbers
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Exception in thread "main" Process exited with exit code 1.
    

提交回复
热议问题