aspose file tools*
The moose likes Java in General and the fly likes difference between error and runtime exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "difference between error and runtime exception" Watch "difference between error and runtime exception" New topic
Author

difference between error and runtime exception

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54

please explain
difference between error and runtime exception
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54


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

Joined: Mar 04, 2009
Posts: 477
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Jan 03, 2008
Posts: 54

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: May 31, 2010
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

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 ...



http://plainoldjavaobject.blogspot.in
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jun 08, 2010
Posts: 384

i think i should have given few more examples ...
Dhawal Mehta
Greenhorn

Joined: Jun 13, 2010
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between error and runtime exception