Exception Handling - Java

  • Error or Exception
    • Compile-time or Run-time
  • Exception Handling
    • To continue the flow of program even exception occurred during runtime
  • Types
    • Check
      • Throws by compiler at compile time
      • Eg = FileNotFoundException
    • Unchecked
      • Throws by compiler at run time
      • Eg = NullPointerException, ArithmeticException, IllegalArgumentException, IndexOutOfBoundsException, IllegalStateException
    • Error
      • Eg = StackOverFlowError, OutOfMemoryError
  • Command
    • Try & Catch => By Developer

        try {
          statement;
        } catch (ExceptionName var) {
          var.printStackTrace();
          var.getMessage();
        } finally {
          // Used to release Extra resources
        }
      
    • User Defined => You as a designer would like to throw exception to instruct the developer to handle if really wants, Stops the program

        // App.java
        public static int functionName() throws InputException {
          if (condition) throw new ExceptionName("")
          else statement;
        }
      
        // ExceptionName.java
        public class ExceptionName extends Exception {
          public ExceptionName(String msg) {
            super(msg);
          }
        }
      
Share: