37

Java Exception Handling – A Complete Reference to Java Exceptions

 5 years ago
source link: https://www.tuicool.com/articles/hit/JfQjInU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ueM3Inf.png!web Basically,  an  Error  is used by the Java run-time system (JVM) to indicate errors that are associated with the run-time environment (JRE).  StackOverflowError is an example of such an error. Whereas E xception is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. 

Now that you know what errors and exceptions are, let’s find out the basic difference between them. Take a look at the below table which draws a clear line between both of them.

Errors Exceptions 1. Impossible to recover from an error 1. Possible to recover from exceptions 2. Errors are of type ‘unchecked’ 2. Exceptions can be either ‘checked’ or ‘unchecked’ 3. Occur at runtime 3. Can occur at compile time or run time 4. Caused by the application running environment 4. Caused by the application itself

Now,we will dive deeper into exceptions and see how they can be handled. First, let’s see the different types of exceptions.

  • Checked Exception

    It is an exception that occurs at compile time, also called compile time exceptions. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using  throws  keyword.

  • Unchecked Exception
    It is an exception that occurs at the time of execution. These are also called  Runtime Exceptions.   In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to specify or catch the exceptions.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK