Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all, i have one doubt about exception..I refered lot of material and i found that "Exception is a runtime error"is it true?.If so compiler will not interfere with runtime error.Then what about checked exeception which compile will shout
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have it backwards: Anything other than RuntimeException and its subclasses are checked exceptions. In other words, only RuntimeException and its subclasses are unchecked exceptions, everything else is a checked exception.
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm agree with Ravi. In Java you have two Exeption type : the uncheck exeption and the checked exception. you must manage all Throwable in your code except unchecked exception (RuntimeException). I show you a simple schema of http://www.artima.com/designtechniques/exceptionsP.html
 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You have it backwards: Anything other than RuntimeException and its subclasses are checked exceptions. In other words, only RuntimeException and its subclasses are unchecked exceptions, everything else is a checked exception.

I think you did not understand my question or i may not be express properly....Here i am expressing again try to clear about my doubt..
I found in lot of material that Exception is a run time error which means compiler does not recognize run time errors.But when we declare Checked Exception which is a type of Exception.Compiler will not allow us to leave without handling that exception.......My doubt is If the Exception by definition is run time exception how the compiler interfere with checked exception(which is run time error)
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources (<--click) so that we can understand what exactly it is you're reading; I'm pretty sure you're not understanding it correctly. Read the JavaDoc for java.lang.Exception carefully, then see some of the results of this search: java checked vs unchecked exceptions for more details.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:My doubt is If the Exception by definition is run time exception how the compiler interfere with checked exception(which is run time error)

Your understanding that "Exception by definition is run time exception" is WRONG.

I think you are confusing the fact that exceptions occur when you run your Java code (that is, they occur at run time) versus the compiler forcing you to handle checked exceptions by generating a compile-time error when you don't. As I said before -- and again, please read the java.lang.Exception JavaDocs -- Exception is a checked exception, therefore you need to either catch it or declare it with a throws clause if you want your code to compile without errors. Only RuntimeException and its subclasses are unchecked exceptions and the Java compiler doesn't care whether or not you catch these or declare them with a throws-clause.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a few things so you can get your terminology right:

1. Compiler error or compile-time error - Occurs when the Java compiler detects something wrong with your Java code - you have to fix these programming mistakes before you can run your code. Note that these are errors in the sense that they are MISTAKES the programmer has made in writing the code; it has nothing at all to do with java.lang.Error

2. Exceptions - thrown at run time; that is, while your successfully compiled Java code is being executed in the JVM.

3. Unchecked exceptions - java.lang.RuntimeException and its subclasses - I have already explained these above. See also: the JavaDocs for these

4. Checked exception - from the JavaDocs for java.lang.Throwable: "For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions." This means that the Java compiler will force you to either catch them or declare them in a throws-clause.

I hope this helps clarify things for you.
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We may not be clearer :
"Exception is a runtime error"
is false if Exception is not a RuntimeException

Like say Junilu, RuntimeException and it's subclasses are unchecked exception.

Other Exception are checked exception.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's some confusion here based on the intepretation of "run time error". I think the original statement was intended to be the equivalent of Junilu's point no.2, and the confusion is related to the different meaning of RuntimeException.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Errors are unchecked exceptions
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:and Errors are unchecked exceptions


Just to be perfectly clear on this point, the JavaDocs say that "for the purpose of compile-time checking of exceptions, Error and its subclasses are regarded as unchecked exceptions."

However, java.lang.Error and it's subclasses are a whole 'nother class hierarchy, separate from Exception and its subclasses; technically, Error is a Throwable, not an Exception.

(Take note of capitalization above: there is a distinct difference between "Exception" with a capital 'E' and "exception" with a lowercase 'e'. The former refers to java.lang.Exception, the class, while the latter is a general term)
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is a perfect example of what Robert C. Martin means when he says "There will always be code." Regular prose and writing is loose, full of ambiguity, and open to different interpretations. Programming languages are very precise and compilers have a very deterministic way of interpreting what the programmer is trying to say. There's no "I said this but I really meant that" reasoning with a compiler. So for those waiting around for AI and natural language to replace people in the business of programming, I would say "Don't hold your breath just yet."
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi kapa wrote:But when we declare Checked Exception which is a type of Exception.Compiler will not allow us to leave without handling that exception...

And even that statement is wrong. The compiler will not allow you to leave without handling that exception UNLESS the method that contains it is declared to throw the exception.
It may seem a small point to you, but its BIG to the compiler.

Winston
 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:This thread is a perfect example of what Robert C. Martin means when he says "There will always be code." Regular prose and writing is loose, full of ambiguity, and open to different interpretations. Programming languages are very precise and compilers have a very deterministic way of interpreting what the programmer is trying to say. There's no "I said this but I really meant that" reasoning with a compiler. So for those waiting around for AI and natural language to replace people in the business of programming, I would say "Don't hold your breath just yet."
.Thanks for the great information guys especially junilu now i understood the concept of exception and all related..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic