• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error and Exception

 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the difference between error and exception. Somebody says the error is thrown by JVM and the exception is thrown by programmers but what about the RuntimeException (subclasses thrown by JVM) and AssertionError (thrown by programmers for debugging purpose)???


 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Search First. I am sure, you will find many valuable replies.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions: Exceptions are any error or any invalid conditions that occur during the execution of program whenever any exception occure an exception object is created and thrown that contains the informatin about the information like the name of that exception or the complete stack trace which provides the information about all the methods that have been called till the time exception occur.
Exceptions can be of two type
Unchecked exceptions : All the exceptions that come under RuntimeExceptions are unchecked exceptions. These exceptions are used to denote any error that exist in the program logic. Any method that can throw the unchecked exceptions does not need to provide a explicit exception handling mechanism for those exceptions. .(do not need to be handled explicitly)

Checked Exceptions: All the exceptions that are subclasses of Exception except Runtime exception are checked exceptions. These exceptions are generally used to represent any invalid condition that occur during the execution of program. Any method that may throw the checked exception should provide a explicit exception handling mechanism to deal with those exceptions otherwise there will be a compile time error.(need to be handled explicitly)

Error: Error defines the exceptions that are not expected to be caught by the program. Like stack Overflow. Recovery from them is difficult or impossible.
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhakar Sharma wrote:. . . Somebody says the error is thrown by JVM and the exception is thrown by programmers . . .
They are mistaken. But it is possible intentionally to throw an Exception or Error or Throwable with the throw keyword.
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhakar Sharma wrote: . . . AssertionError (thrown by programmers for debugging purpose) . . .
It is not normal to throw an AssertionError, but to use the keyword assert, which you correctly say is used for debugging, or more precisely for checking correctness of the program. I think the AssertionError is a bit of a "cheat" because catch (Exception e) . . . is a common programming construct and the Error passes that, a bit like an unchecked Exception.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell,

But my question is not answered ie the difference between them
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sudhakar

Error and Exception are subclasses of class Throwable which deals basically with exceptions in java .

Exception handling is a mechanism in which java enables you to deal with conditions that might cause your program to stop.

Dealing with it will enable you to stop the program from stopping execution at runtime and continue with further execution .

You can refer to the documentation for understanding class Throwable and its subclasses Error and Exception.

 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Java™ Tutorials!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic