Differences between Runtime/Checked/Unchecked/Error/Exception

前端 未结 8 892
走了就别回头了
走了就别回头了 2020-11-27 05:17

What are the Runtime exceptions and what are Checked/Unchecked Exceptions and difference between Error/Exception.Why these many types? Instead Java may simply follow a simpl

相关标签:
8条回答
  • 2020-11-27 06:01

    Difference between Checked and unchecked exceptions:

    We have many differences between checked and unchecked exception but all the differences originate from once basic consideration that whether the exception is solvable by compiler or not.

    Points to remember are:

    [1] Checked exception means Compiler checked Exceptions. It means that compiler mandates that such exception to be handled by try-catch block or throws keyword.

    [2] Unchecked exceptions are the ones for which compiler doesn’t provides any mandate as they can be resolved by developer by coding/programing as the control flow is controllable like in ArithmeticException, NullPointerException ArrayIndexOutOfBoundsException, IllegalArgumentException ,etc.

    I call it “Exception-Identity-Test” where you take any random exception from java doc and just ask it one question. “Hey Exception! Can you be solved programmatically?”

    If the exception says YES then it is an Unchecked Exception as this can be solved by either code change or resolving some calculation mistake etc.

    On the other hand if the Exception says No then this is Checked Exception as in checked Exception control flow goes out of our code like if someone changes Database passwords or someone unplugs the network cable ,connection timeout (ConnectException), some resource is not found (FileNotFoundException, ClassNotFound), SQLException, InvocatonTargetException etc. These ones cannot be resolved by programming

    0 讨论(0)
  • 2020-11-27 06:06

    Since I am a new Java developer, I have also faced some difficulties for distinguishing and dealing with different types of exceptions. That is why I have made a short note on this topic, and whenever I get confused I go through it. Here it is with the image of the Throwable class hierarchy:
    Throwable Class Hierarchy

    [image courtesy of JavaTpoint].

    There are three key classes to remember here: Throwable, Exception and Error. Among these classes Exception can be divided into two types: "Checked Exception" and "Unchecked Exception".

    Checked Exception:

    • These are the classes that extend Throwable except RuntimeException and Error.
    • They are also known as compile time exceptions because they are checked at compile time, meaning the compiler forces us to either handle them with try/catch or indicate in the function signature that it throws them and forcing us to deal with them in the caller.
    • They are programmatically recoverable problems which are caused by unexpected conditions outside the control of the code (e.g. database down, file I/O error, wrong input, etc).
    • Example: IOException, SQLException, etc.

    Unchecked Exception:

    • The classes that extend RuntimeException are known as unchecked exceptions.
    • Unchecked exceptions are not checked at compile-time, but rather at runtime, hence the name.
    • They are also programmatically recoverable problems but unlike checked exception they are caused by faults in code flow or configuration.
    • Example: ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException, etc.
    • Since they are programming errors, they can be avoided by nicely/wisely coding. For example "dividing by zero" produces an ArithmeticException, which can be avoided by a simple check on the divisor. Similarly we can avoid NullPointerException by simply checking the references: if (object != null) or even using better techniques.

    Error:

    • Error refers to an irrecoverable situation that is not being handled by a try/catch.
    • Example: OutOfMemoryError, VirtualMachineError, AssertionError, etc.

    Why are these many types?

    In addition to Stephen C's answer I want to say: exception handling is a relatively expensive operation in Java. We should not put all exceptional situation in a try/catch block. Excessive use of try/catchs may hamper program performance.

    In conclusion, Exceptions should be handled programmatically whenever possible. On the other hand, we cannot handle Errors, so these might be some logical reasons why there are many types of exceptions.

    0 讨论(0)
提交回复
热议问题