This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Exception and Error

 
Zubair Mushtaq
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have confusion about exception and error.suppose if u make a software so u must b having Exception handling in that.so wat ab errors?can they b handled like exceptions? if yes then what is the differece between both of these?
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to declare Exceptions you may throw in your throws clause. You don't have to do this with Errors. Other than that, they are identical.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to be a bit more specific about what you mean by "error". Notice that Error is a Java class, too, in the following hierarchy:

Throwable <-- Error <-- ...
Throwable <-- Exception <-- ...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a perfect world, and in a nutshell...
Errors represent problems that aren't typically handled by a program. They represent severely bad situations that should not occur, and that a programmer should not try to recover from.
Sometimes errors occur because of (external) conditions that cannot be controlled or effected by the running Java program.
Exceptions come in too basic forms: Runtime Exceptions (aka Unchecked Exceptions) and Checked Exceptions.
Runtime Exceptions typically are not meant to be handled by a program. They often represent programmer errors, that should reveal themselves at run time and that should be fixed by the programmer correcting the code problem.
Checked Exceptions typically represent problem conditions outside of a program's control, but should be recovered from by the program.
For more information, a good place to learn lots of different and good ideas on exceptions in Java is at http://c2.com/cgi/wiki?JavaExceptionHandling
Does that help?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that link - some solid discussion about exception issues. This is a controversial area and I was surprised to see almost nothing newer than 2000, tho.
Did anybody see the Throws Excepiton By Default page? I think I'm for it. I've been doing a lot of stuff that reads streams lately, and letting any exceptions percolate quite a way up the stack to a higher level class for any handling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic