Difference between error and exception?
5 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sriram_Sundararajan
Posted On:   Friday, April 18, 2003 02:07 AM

hi all
what is the difference between error and exception in java. While running the application the programmer usually gets runtime exception and compile time error. Can we get error during runtime? For example out of memory error occurs during runtime, how can i handle that?If i can handle out of memory error how can i handle rest of the errors. If i can't handle the Error, then what is the purpose sun has given the Error class?

thanks and regards

sri

Re: Difference between error and exception?

Posted By:   Simon_Ablett  
Posted On:   Wednesday, April 23, 2003 05:52 AM

There are compile time and runtime errors.

Compilation errors occur at compilation time and can be programmatic (i.e. syntactically incorrect code) or environmental (i.e. insufficient disk space etc.).

Runtime errors obviously occur when an application is invoked. They are sub-classed into checked and unchecked types. Unchecked errors are those that a program cannot reasonably be expected to recover from (!?!?!) and should not therefore be caught. There are various types of unchecked errors of which two representations are RuntimeException and Error. RuntimeExceptions are usually indicative of a programmatic error (e.g. not checking for unitialised variables before using them etc). Errors on the other hand usually indicate environmental problems such as the JVM running out of memory etc. Checked exceptions can usually be handled in some way (if only by reporting them to the user) and should always be caught.

Hope that this clarifies things.

Re: Difference between error and exception?

Posted By:   cem_tekin  
Posted On:   Tuesday, April 22, 2003 03:44 PM

a compile time error occurs when the written code has errors in it. for example trying to assign a string to an int.


an exception occurs while the program is running (runtime error = exception). these occur mostly due to passing wrong arguments, but may occur for other reasons too. it is possible to catch these (try-catch-finally blocks).


it is easy to catch compile time errors because your java file won't compile without correcting them.


exceptions are harder to correct.


cem.

Re: Difference between error and exception?

Posted By:   cem_tekin  
Posted On:   Tuesday, April 22, 2003 03:43 PM

a compile time error occurs when the written code has errors in it. for example trying to assign a string to an int.


an exception occurs while the program is running (runtime error = exception). these occur mostly due to passing wrong arguments, but may occur for other reasons too. it is possible to catch these (try-catch-finally blocks).


it is easy to catch compile time errors because your java file won't compile without correcting them.


exceptions are harder to correct.


cem.

Re: Difference between error and exception?

Posted By:   cem_tekin  
Posted On:   Tuesday, April 22, 2003 03:43 PM

a compile time error occurs when the written code has errors in it. for example trying to assign a string to an int.


an exception occurs while the program is running (runtime error = exception). these occur mostly due to passing wrong arguments, but may occur for other reasons too. it is possible to catch these (try-catch-finally blocks).


it is easy to catch compile time errors because your java file won't compile without correcting them.


exceptions are harder to correct.


cem.

Re: Difference between error and exception?

Posted By:   Stephen_McConnell  
Posted On:   Friday, April 18, 2003 05:23 AM

One compiles a java program to get a class file which executes. You get compile Errors then. These are separate from Runtime Errors and Exceptions


If you look at the Java Docs for any one of the versions of the SDK found on the Sun site; and check out the java.lang package, you will notice both Runtime Errors and Runtime Exceptions! This can be confusing.


You can use the exception handling of a


try{
// code to test
}catch(){
// error/exception handling stuff.
}

to process both Runtime Errors and Exceptions.


Looking at the JavaDoc; and comparing the difference between Errors and Exceptions, you might notice that the Errors usually have to do with something that has happened in the hanling of Class Loading or something that the JVM cannot handle. Exceptions usually happen because of a coding error...


How to handle errors and exceptions has been the topic of dozens of artiles and books. I would suggest looking at the archives in the Javaworld site. It is a great resource for all things java; and has good articles on the stuff that is cutting edge...


Hope this helps.


Stephen McConnell

About | Sitemap | Contact