Error Exception Difference Java
Error Vs Exception In Java pramodbablad April 15, 2015 3 Both java.lang.Error and java.lang.Exception classes are sub classes of java.lang.Throwable class, but there exist some significant differences between them. java.lang.Error class represents the errors which are mainly caused by the environment in which application is running. For example, OutOfMemoryError occurs when JVM runs out of memory or StackOverflowError occurs when stack overflows. Where as java.lang.Exception class represents the exceptions which are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object or ClassCastException occurs when an application tries to cast incompatible class types. In this article, we will discuss the differences between Error and Exception in java. Below is the list of differences between Error and Exception in java. Error Vs Exception In Java : 1) Recovering from Error is not possible. The only solution to errors is to terminate the execution. Where as you can recover from Exception by using either try-catch blocks or throwing exception back to caller. 2) You will not be able to handle the Errors using try-catch blocks. Even if you handle them using try-catch blocks, your application will not recover if they happen. On the other hand, Exceptions can be handled using try-catch blocks and can make program flow normal if they happen. 3) Exceptions in java are divided into two categories - checked and unchecked. Where as all Errors belongs to only one category i.e unchecked. Click here for more info on Checked and Unchecked Exceptions. 4) Compiler will not have any knowledge about unchecked exceptions which include Errors and sub classes of RunTimeException because they happen at run time. Where as compiler will have knowledge about checked Exceptions. Compiler will force you to keep try-catch blocks if it sees any statements which may throw checked exceptions. 5) Exceptions are related to application where as Errors are related to environment in which application is running. Below is the quick recap of above points. Errors Exceptions Errors in java are of type java.lang.Error. Exceptions in java are of type java.lang.Exception. All errors in java are unchecked type. Exceptions include both checked as well as unchecked type. Errors happen at run time. They will not be known to compiler. Checked exceptions are known to compiler where as unchecked exceptions are not known to compiler because they occur at run time. It is impossible to recover from errors. You
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What is difference between Errors and Exceptions? [duplicate] up vote 69 down vote http://javaconceptoftheday.com/difference-between-error-vs-exception-in-java/ favorite 34 Possible Duplicate: Differences betweeen Exception and Error How can I differentiate between Errors and Exceptions in Java? java exception share|improve this question edited Apr 28 '11 at 4:31 Michael Petrotta 44.5k11108151 asked Apr 28 '11 at 4:24 hiren gamit 41331322 marked as duplicate by Bill the Lizard Apr 28 '11 at 11:23 This question has been asked before and already has an answer. If those answers do http://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions not fully address your question, please ask a new question. add a comment| 4 Answers 4 active oldest votes up vote 95 down vote accepted An Error "indicates serious problems that a reasonable application should not try to catch." while An Exception "indicates conditions that a reasonable application might want to catch." Error along with RuntimeException & their subclasses are unchecked exceptions. All other Exception classes are checked exceptions. Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically. Examples include FileNotFoundException, ParseException, etc. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the caller On the other hand we have unchecked exceptions. These are those exceptions that might not happen if everything is in order, but they do occur. Examples include ArrayIndexOutOfBoundException, ClassCastException, etc. Many applications will use try-catch or throws clause for RuntimeExceptions & their subclasses but from the language perspective it is not required to do so. Do note that recovery from a RuntimeException is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions. Errors are also unchecke
Error and Exception are derived from java.lang.Throwable in Java but main difference between Error and Exception is kind of error they represent. java.lang.Error represent errors which are generally can not be handled and usually http://www.java67.com/2012/12/difference-between-error-vs-exception.html refer catastrophic failure e.g. running out of System resources, some examples of Error in Java are java.lang.OutOfMemoryError or Java.lang.NoClassDefFoundError and java.lang.UnSupportedClassVersionError. On the other hand java.lang.Exception represent errors which can be catch and dealt e.g. IOException which comes while performing I/O operations i.e. reading files and directories. Clear understanding of Error and Exception is must for any serious Java programmer and good programming and debugging skills are required to error exception overcome issues which caused Error and Exception in Java. Apart from its must have knowledge in Java application development, difference between Error and Exception is also a popular questions on Java interviews related to Exception handling, similar to difference between throw and throws in Java. In this Java article we will briefly see major difference between Error and Exception in Java which include both syntactical and logical difference. Error vs error exception difference Exception in Java Here is my list of notable difference between Error vs Exception in Java. 1) As I said earlier, Main difference on Error vs Exception is that Error is not meant to catch as even if you catch it you can not recover from it. For example during OutOfMemoryError, if you catch it you will get it again because GC may not be able to free memory in first place. On the other hand Exception can be caught and handled properly. 2) Error are often fatal in nature and recovery from Error is not possible which is different in case of Exception which may not be fatal in all cases. 3) Unlike Error, Exception is generally divided into two categories e.g. checked and unchecked Exceptions. Checked Exception has special place in Java programming language and require a mandatory try catch finally code block to handle it. On the other hand Unchecked Exception, which are subclass of RuntimeException mostly represent programming errors. Most common example of unchecked exception is NullPointerException in Java. 4) Similar to unchecked Exception, Error in Java are also unchecked. Compiler will not throw compile time error if it doesn't see Error handled with try catch or finally block. In fact hand