• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between error and runtime exception

 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please explain
difference between error and runtime exception
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example: Mistyped keywords can contribute to errors or incorrect method names.

@Rob: Now that you mention it... The above would be a Checked Excpetion wouldn't it?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error can also mean java.lang.Error or a subclass, which is an indication that something has gone horribly wrong - you're out of memory, you have infinite recursion, you're missing a required class or library, etc.
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


somanath i dont agree with you. my code compiles fine there are no typos . so what is the exact difference between error and runtime exception
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kish kumar wrote:

somanath i dont agree with you. my code compiles fine there are no typos . so what is the exact difference between error and runtime exception


Ooops... edited my orginal post by mistake!!

Well check what Rob said, and it would be so much better f you post your Error Message in the thread.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somnath was right though. The term "error" is broad, and can mean both an Error, an Exception or a compiler error. Only the first and last are correct, the second is not an error but an exception.
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob you explained some forms of Error. but what is the exact difference between error and runtime exception?

The term "error" is broad, and can mean both an Error, an Exception or a compiler error.


then why java gave us different classes such as Exception ,Error, RuntimeException ?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the difference between RuntimeException and Exception I suggest you search around this forum. It's a question asked many, many times before.
 
Adwin Lorance
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok , let me share what I think ...

There are 2 types of exception
Checked and Unchecked Exception.

Checked Exception : Object of type Exception or its sub class , except the runtime exception.
Unchecked Exception : Object of Type Runtime Exception and its sub type.

Compiler forces you to handle/declare any checked exceptions.

Compiler does not force you to handle/declare any unchecked exceptions , as you cannot predict whether an unchecked exception can occur.

But still , you can handle it.

Error - It is unchecked , as you cannot predict when an Error can occur.
Also , you cannot handle it.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost right. You actually can catch any Throwable, so also Errors. It's not advised to do so in most occurrences, but it's still possible.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another swing at it... You can extend and throw and catch any Throwable. It's
just that the compiler is more relaxed about Throwble.Exception.Runtime and
Throwable.Error; just those two. Everything else that Is-A Throwable is checked.

Jim ... ...
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think exceptions are the conditions that arise when something goes wrong or opposing to the rules and guidelines defined for java programs ...

as if you say

IOException ... there must be a file if you are trying to open it.
SQLException ... the SQL engine must be setup and running.

and error unlike an exception is something ... which might or might not let your program to compile ...

as if you say
syntax error ... the code syntax is not according to the java libraries and classes
logical error ... well this is something which will only do something else rather that doing what it intends to do.

... i hope i'm right at some places ...

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly. Error and its subclasses are intended to flag runtime conditions
that are beyond the programmer's control; a hardware fault, for example.
You should extend and use Error for these unrecoverable, program crashing
problems. The compiler does not check these.

Throwable.Exception.Runtime exceptions can often be pogrammer controlled
but do not arise until the program runs; NullPointerException for example.
Though it would be helpful, the compiler does not check these either. It can't
because they appear only at runtime.

Jim ... ...
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i should have given few more examples ...
 
Dhawal Mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime exception are those condition which occurs when the program is running and some thing goes wrong e.g. ArrayOutOfBoundException when its elements exceede Array size. So this exceptions can be handled through Exception Handling.

Error is a condition which cannot be handled by our program and it is also not handled by java run time environment e.g Stack overflow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic