difference between errors and unchecked exceptions in java?

后端 未结 7 1088
时光取名叫无心
时光取名叫无心 2020-12-16 18:22

As we know if any error or any unchecked exception occurs then our program will halt, then what are the differences between those?

相关标签:
7条回答
  • 2020-12-16 18:41

    Difference between errors and unchecked exceptions in java?

    Unchecked exceptions = class RuntimeException and its subclasses + class Error and its subclasses

    There fore the error are part of unchecked exception. Unchecked exception also contains RuntimeException and its subclasses.

    0 讨论(0)
  • 2020-12-16 18:45

    From the JavaDoc:

    An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

    RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

    So, the only difference technically is that they are two different classes. You will only catch both if you declare

    catch (Throwable e) { }
    

    But there is a big difference in how they are intended to be used. Unchecked exceptions (RuntimeExceptions) are intended to deal with programming errors and other unexpected problems, but should be caught and handled in the application. Errors are intended to represent problems that the program cannot deal with, such as running out of memory.

    0 讨论(0)
  • 2020-12-16 18:46

    Unchecked Exception:

    • The classes that extend RuntimeException are known as unchecked exceptions
    • Unchecked exceptions are not checked at compile-time rather they are checked at runtime.And thats why they are also called "Runtime Exception"
    • 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 error, they can be avoided by nicely/wisely coding. For example "dividing by zero" occurs ArithmeticEceeption. We can avoid them by a simple if condition - if(divisor!=0). Similarly we can avoid NullPointerException by simply checking the references - if(object!=null) or using even better techniques

    Error:

    • Error refers irrecoverable situation that are not being handled by try/catch

    • Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.

      This question may also be helpful in this context - Runtime/Checked/Unchecked/Error-Exception

    0 讨论(0)
  • 2020-12-16 18:46

    The term «reasonable» is relative. As it is, also, the term «application»

    As middleware developer, I'm used to deal with unchecked exceptions thrown, or simply ignored, by application developers

    What is reasonable to be catched by an application is is a subset of what is reasonable to be catched by its underlying infrastructure (that is, itself, a kind of application)

    That's where unchecked exceptions are different from errors. An unchecked can be catched by the infrastructure (i.e. to be registered in a.database) but ignored by an application

    Registering an error could be impossible because there could be no JVM to run the registering code (or even the catching code), that's why it's not reasonable to catch them

    As an aside, checked exceptions are IMHO overused. Catching them to promote to runtime exceptions is too much usual

    0 讨论(0)
  • Just as checked exceptions are useful for signaling when your methods cannot fulfill their contract, there are other errors outside of your control that can occur that prevent the Java virtual machine from fulfilling its specification, such as when memory is exhausted. Since you can't plan for such errors ahead of time, you would have to catch them everywhere, which defeats the principle of maintaining uncluttered code. Therefore, these errors are unchecked exceptions, meaning exceptions that you don't have to include in a throws clause. You are welcome to catch them (well, some of them), but the compiler won't make you do it. Unchecked exceptions fall into two categories: those that extend RuntimeException, and those that extend Error. I realize that I said earlier that classes inheriting from class Exception are checked exceptions, but that's only half true: the whole truth is that classes in the Exception hierarchy other than those in the RuntimeException sub-hierarchy are checked exceptions.

    Exceptions that extend RuntimeException represent errors that you may want to handle, although you're not required to.

    As I stated above, RuntimeExceptions are not checked because making you advertise them would have no effect on establishing the correctness of your methods, and would unnecessarily clutter your otherwise very readable code. Exceptions derived from the Error class, on the other hand, are unchecked because you never want to catch them! Error exceptions are severe errors that require shutting down the virtual machine. InternalError, which I used above, extends VirtualMachineError, which is an Error subclass. OutOfMemoryError is another obvious severe error, but there are others, like StackOverflowError, and various LinkageErrors. A linkage error means something has gone amiss when the class loader tried to load a class for execution, and commonly occurs either because some external source has introduced malicious code in an attempt to circumvent Java's security mechanism, or it came from an out-of-spec byte code generator.

    0 讨论(0)
  • 2020-12-16 18:59

    From the Error Javadoc:

    An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

    Versus the Exception Javadoc

    The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

    So, even though an unchecked exception is not required to be caught, you may want to. An error, you don't want to catch.

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