Java - Exception Handling


Avoid the interruption in between normal flow of the application in other words to control the flow of the application.






Types:
1. Compile Time Exception (Checked Exception):
                This will happened at compile time. It should be handled by programmer.
               SQL Exception
                IO Exception
private static String filepath = "D:\User\guest\Desktop\File2.txt";
 
    public static void main(String[] args) {
        BufferedReader br1 = null;
        String curline;
 
        try {
            br1 = new BufferedReader(new FileReader(filepath));

                ClassNotFound Excepection
private static final String CLASS_TO_LOAD = "main.java.Utils";
 
    public static void main(String[] args) {
        try {
            Class loadedClass = Class.forName(CLASS_TO_LOAD);


2. Run Time Exception (Un Checked Exception):
                This will occur at program run time. Like logical error.

               Null Pointer Exception
        String a1 = null; // null value
            System.out.println(a1.charAt(0));

                ArrayIndexOutOfBoundary Exception
        int b[] = new int[6];
            b[8] = 2; // we are trying to access 9th element in an array of size 7

                Numberformat Exception
          int n = Integer.parseInt ("Test") ;
            System.out.println(n);

                Arithmetic Exception:
               int p = 30, q = 0;
            int r = p/q;  // It cannot be divided by zero
                
Error:
                Error is nothing but based on system issue. Like java memory space. Or other memory space issue.

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab