wood burning stoves*
The moose likes Java in General and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
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

Joined: Feb 26, 2001
Posts: 4777
    
    8

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Guillaume Jourdan
Ranch Hand

Joined: Jul 24, 2012
Posts: 47

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

Joined: Jan 05, 2012
Posts: 87
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

Joined: Feb 26, 2001
Posts: 4777
    
    8

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

Joined: Feb 26, 2001
Posts: 4777
    
    8

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

Joined: Feb 26, 2001
Posts: 4777
    
    8

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

Joined: Jul 24, 2012
Posts: 47

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

Joined: Apr 06, 2010
Posts: 4425
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

and Errors are unchecked exceptions
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4777
    
    8

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

Joined: Feb 26, 2001
Posts: 4777
    
    8

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

Joined: Mar 17, 2011
Posts: 8043
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception