Difference between Unchecked exception or runtime exception

后端 未结 8 1528
忘了有多久
忘了有多久 2020-11-29 21:55

This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.

相关标签:
8条回答
  • 2020-11-29 22:28

    Note: a RuntimeException IS an unchecked exception

    An unchecked exception would be one that is known to be possible at a point in the execution but is not caught, for example a NullPointerException is always a possibility if you don't check for them and will cause your program to terminate. You could check for it by wrapping code in try-catch, but this is not enforced (unlike a checked exception that will enforce that the exception is handled in some way).

    An error is something that can occur at any point during execution and can't really be caught because it is not eplicitly caused by a particular method call etc. For example an OutOfMemoryError or a StackOverflowError. Both of these could occur at any time and will cause your application to terminate. Catching these errors make no sense as they indicate that something has happened that you won't be able to recover from.

    0 讨论(0)
  • 2020-11-29 22:33

    Error: These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.

    Runtime exception : These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.

    You may want to read this :

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