• Post Reply Bookmark Topic Watch Topic
  • New Topic

Errors and Exceptions  RSS feed

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear frnds,
I have the confusion for Java Error and exceptions. What i know bout error is that something related to JVM, and exception is something that is due to a program error.
Can anybody elaborate on this? Means is there a clear distinction between both these?
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Do you mean checked and unchecked exceptions? Checked exceptions must be catched in code or the code won't compile. Unchecked exceptions are those that are not required to catch, like ArithmeticException, IllegalStateException, IndexOutOfBoundsException. But neverless such exception would kill the program that doesn't catch it. The short story that checked exceptions are derrived from Exception. Unchecked are derrived from RuntimeException.
I hope I understood the question.
Regards
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a difference between an error and an exception in Java. Note that errors are not just JVM problems and exceptions are not just program errors.
An error represents some problem condition that the program likely cannot and should not attempt to correct. It might be something like an OutOfMemoryError or a StackOverflowError, which, respectively, could be caused by a memory leak in the running program (which is a program error) or from some never-ending recursive routine that somebody programmed incorrectly, but these are not conditions that a running program can typically recover from. Or the error could be an InternalError, which may well signal a bug in the JVM implementation.
Exceptions typically represent either program mistakes or problems that a running program should be able to recover from. So, sometimes an exception does represent program error, such as when some method is invoked which hasn't had its preconditions met. Like calling pop on a Stack data structure when the Stack is empty, which causes an EmptyStackException. As mentioned, sometimes an exception represents some problem that a running program couldn't have reasonably avoided, but the running program should still be able to recover from. Such an exception might be an IOException caused by the user of a program ejecting a floppy disk which had a file that the Java program was in the middle of using. The Java program should catch this IOException and somehow handle (recover) the situation. (That recovery might be something simple such as displaying a message of complaint that the file suddenly disappeared, and then terminating the program.)
Are you getting a better idea of these concepts?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!