1

Is this statement/example valid for checked and unchecked exception?

Unchecked Exception: The exceptions that are not checked at compile time are called unchecked exceptions. Example:

 public class UncheckedException {

    public static void main(String[] args) {

            int value = 10/0;
        }
    }

Checked Exception: The exceptions that are checked at compile time are called Checked exceptions. Example:

public class CheckedException {

    public static void main(String[] args) {

        try {
                int value = 10/0;
            } catch (Exception e) {
                System.out.println("Caught " + e);
            }

        }
    }
Saurabh Jhunjhunwala
  • 2,706
  • 2
  • 26
  • 54
bdur
  • 309
  • 1
  • 6
  • 17

1 Answers1

4

No it is not a valid example / illustration. In both cases the exception that is thrown is an unchecked exception.

The difference between checked exceptions and unchecked exceptions is the exception class.

  • ArithmeticException is always an unchecked exception because it extends RuntimeException

  • IOException is a checked exception because it does not extend RuntimeException (or Error).

The fact that you do or don't catch the exception doesn't change its nature.


At the risk of repeating myself:

Unchecked Exception: The exceptions that are not checked at compile time are called unchecked exceptions.

Checked Exception: The exceptions that are checked at compile time are called Checked exceptions.

These are both incorrect definitions.


See also: Java: checked vs unchecked exception explanation

Community
  • 1
  • 1
Stephen C
  • 669,072
  • 92
  • 771
  • 1,162