• Post Reply Bookmark Topic Watch Topic
  • New Topic

Into Exceptions and Confusions ... Part -1  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there ,,
While reading about java exceptions i am both thrilled and confused ... there are 2 short confusions that i would like to clear ....
I am practicing java in eclipse...
/****************************************************************************/

Q1) Consider the following java code :



Lets see the output in eclipse :



Now why is this showing Compilation problem of missing ';' as an exception ?? Is compilation problem a a sort of exception ? Which is it's class then ? Is it checked or unchecked ?

/**********************************************************************************************/

Q2) Consider the following java code :



Let's see the output :



Now , nobody threw this exception , Nor there was any method throwing this exception , Then from where was this exception generated ?? who threw this exception ?? How was it dealt and by whom ??

/*******************************************************************************************************************/

 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1: That exception is because you have tried to run code that failed to compile.
Q2: The JVM. See the docs for the exception http://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to my answer to Q1: A compilation problem is not a type of exception but trying to run uncompilable code is. The exception type is 'Error' - see the API docs http://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll answer both questions:

Q1) The code you posted is not valid Java (as you pointed out in the comment). You cannot run that code under normal circumstances because you cannot compile it. Eclipse tries to be helpful by allowing you to 'run' the code, but it will have replaced the code that does not compile with some of its own code, and the code it substitutes for your code throws that Exception. As an advanced user this may be useful in some situations, but as a beginner this is just going to confuse you. you should probably try compiling your code using the javac command and then running it yourself, at least until you learn how Java works Edit this is incorrect, my bad. As pointed out above it's a Java Error.

Q2) The ArithmeticException is a built in RuntimeException that gets thrown when your code does something that is not valid maths, such as dividing by zero. In your specific example it is obvious that this is going to happen, but in the general case its not possible for the compiler to know what values will be used in the arithmetic expression because the values won't necessarily be known until runtime. This type of exception indicates a bug in your program. You need to program in such a way that makes sure the divisor in a division is never 0. As such you should not be catching this type of Exception. If you ever see it then you need to look at your code and change it so that the Exception can't be thrown.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although on second though that Error maybe is being added in by Eclipse, since javac wouldn't produce that? It would just fail to compile. I'll have a look online.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct Mike, I should have noted that in my reply. It's the IDE's trying to be helpful by producing a class file even though it has compile time errors.

I shouldn't be too hard on the IDE though as it can be a useful feature when you know what you are doing - the problem is it confuses the hell out of novices.
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !!
Please correct me if i am wrong ... This is what in the end i understood from your answers ....

1)Eclipse has implemented its own compiler called as Eclipse Compiler for Java (ECJ). Now , this compiler treats compilation error by reporting exceptions , while javac just fails to compile .
2)Exceptions are not always created by programmers but also by JVM itself .

 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant Kunwara wrote:1)Eclipse has implemented its own compiler called as Eclipse Compiler for Java (ECJ). Now , this compiler treats compilation error by reporting exceptions , while javac just fails to compile .

Not quite. The eclipse compiler (as Mike correctly stated) does it's best to create a class file even when the code is not compilable. Where there is non compilable code it adds a throw new Error() statement with a suitable message explaining why the code wouldn't compile at that point in the code.
When the code compiles you will not see that Exception, you will see compiler error messages though. However, if you ignore the compiler error messages (red triangles or however your IDE shows errors) and try to run the code then you will see the Exception.

Sushant Kunwara wrote:2)Exceptions are not always created by programmers but also by JVM itself .

Yes, things like divide by zero, insufficient memory, class not found etc are all scenarios which only the JVM can be aware of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!