my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception and Error Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception and Error Q" Watch "Exception and Error Q" New topic
Author

Exception and Error Q

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
In the following coding, why compiler ignores exception at line 1 and catches error at line 2.

Is it this, because of line 1 is exception, it will catch at runtime and line 2 is an error so, it will catch at compile time.



"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Nitin,
ArithmeticException will be thrown at runtime. However, the int result which is assigned to byte is caught at compile-time. Hope that helps ya.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The Java spec spells out which specific errors are flagged at compile time. Type safety, uninitialized variables, ambiguous member selection, and narrowing casts are examples.

Sometimes javac looks at literal values, such as in "byte b1 = 1;", which is allowed even though "1" is an int because javac knows that "1" will fit in a byte. javac may even do the arithmetic with literals at compile time, so "byte b3 = 2 / 1;" is allowed but "byte b3 = 1 + 1000;" and "byte b3 = 2 / 0;" are not. But this is done only enough to implement the special rule involving constant initializers of bytes, chars, and shorts, which is part of the check for narrowing casts. "int i = 10/0;" does not require doing the arithmetic to check for narrowing casts because the result of the / will always be an int.

I suspect that the reason Java allows 10/0 to go into execution is to allow students like us to practice handling exceptions. At any rate, that is the rule affecting your example.


Mike Gershman
SCJP 1.4, SCWCD in process
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Very nice explanation Mike, Thank you !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception and Error Q