Check and unchecked Exceptions - Java Object Oriented Design

Java examples for Object Oriented Design:Exception

Introduction

Two basic types of exceptions in Java are checked exceptions and unchecked exceptions:

  • A checked exception is an exception that the compiler requires you to provide for it one way or another. If you don't, your program doesn't compile.
  • An unchecked exception is an exception that you can provide for, but you don't have to.

Demo Code

public class Main {
  public static void uncheckedException() {
    int a = 5;/* www  .  j  a  v  a  2  s . c om*/
    int b = 0;
    int c = a / b; // not catching

  }

  public static void uncheckedException2() {
    int a = 5;
    int b = 0;
    try {
      int c = a / b; // but you try it anyway
    } catch (ArithmeticException e) {
      System.out.println("Oops, you can't divide by zero.");
    }
  }
}

Related Tutorials