Skip to main content

Top-10 exceptions


Top-10 Exceptions:
• Exceptions are divided into two types. They are:
1) JVM Exceptions:
2) Programmatic exceptions:
JVM Exceptions:
The exceptions are raised automatically by the JVM whenever a particular event occurs.
Example:
1) ArrayIndexOutOfBoundsException(AIOOBE)
2) NullPointerException (NPE).
Programmatic Exceptions:
• The exceptions which are raised explicitly by the programmer (or) by the API developer are called programmatic exceptions.
Example:
1) IllegalArgumentException(IAE).
1) ArrayIndexOutOfBoundsException:
• It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM whenever we are trying to access array element with out of range index.
Example:
     class Test{
               public static void main(String[] args){
               int[] x=new int[10];
               System.out.println(x[0]);//valid
               System.out.println(x[100]);//AIOOBE
               System.out.println(x[-100]);//AIOOBE
           }
       }
2) NullPointerException:
• It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM, whenever we are trying to call any method on null.
Example:
       class Test{
                public static void main(String[] args){
                String s=null;
                System.out.println(s.length());R.E: NullPointerException
          }
     }
3) StackOverFlowError:
• It is the child class of Error and hence it is unchecked. Whenever we are trying to invoke recursive method call JVM will raise StackOverFloeError automatically.
Example:
        class Test
             {
                    public static void methodOne()
                          {
                              methodTwo();
                          }
                    public static void methodTwo()
                        {
                             methodOne();
                        }
                    public static void main(String[] args)
                      {
                          methodOne();
                      }
             }
Output:
Run time error: StackOverFloeError
4) NoClassDefFound:
It is the child class of Error and hence it is unchecked. JVM will raise this error automatically whenever it is unable to find required .class file.
Example: java Test
• If Test.class is not available. Then we will get a NoClassDefFound error.
5) ClassCastException:
• It is the child class of RuntimeException and hence it is unchecked. Raised automatically by the JVM whenever we are trying to typecast parent object to child type.
Example:
6) ExceptionInInitializerError:
It is the child class of Error and it is unchecked. Raised automatically by the JVM, if any an exception occurs while performing static variable initialization and static block execution.
Example 1:
    class Test{
         static int i=10/0;
          }
Output:
Runtime exception:
• Exception in thread "main" java.lang.ExceptionInInitializerError
Example 2:
       class Test{
                static {
                       String s=null;
                      System.out.println(s.length());

                    }
             }
Output:
Runtime exception: Exception in thread "main" java.lang.ExceptionInInitializerError
7) IllegalArgumentException:
It is the child class of RuntimeException and hence it is unchecked. Raised explicitly by the programmer (or) by the API developer to indicate that a method has been invoked with inappropriate argument.
Example:
         class Test{
              public static void main(String[] args){
                    Thread t=new Thread();
                        t.setPriority(10);valid
                           t.setPriority(100);invalid
                         }
                  }
Output:
Runtime exception
Exception in thread "main" java.lang.IllegalArgumentException.
8) NumberFormatException:
It is the child class of IllegalArgumentException and hence is unchecked. Raised explicitly by the programmer or by the API developer to indicate that we are attempting to convert string to the number. But the string is not properly formatted.
Example:
      class Test{
                public static void main(String[] args){
                      int i=Integer.parseInt("10");
                          int j=Integer.parseInt("ten");
                     }
              }
Output:
Runtime Exception
• Exception in thread "main" java.lang.NumberFormatException: For input string: "ten"
9) IllegalStateException:
• It is the child class of RuntimeException and hence it is unchecked. Raised explicitly by the programmer or by the API developer to indicate that a method has been invoked at an inappropriate time.
Example:
• Once the session expires we can’t call any method on the session object otherwise we will get IllegalStateException
HttpSession session=req.getSession();
System.out.println(session.getId()); 
session.invalidate();
System.out.println(session.getId());illgalstateException
10) AssertionError:
• It is the child class of Error and hence it is unchecked. Raised explicitly by the programmer or by API developer to indicate that Assert statement fails.
Example: 

Comments

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca