File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why there are exceptions of 2 types? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why there are exceptions of 2 types?" Watch "why there are exceptions of 2 types?" New topic
Author

why there are exceptions of 2 types?

Guna sekar
Ranch Hand

Joined: May 18, 2010
Posts: 32
Why there are exceptions of two types - checked vs unchecked exceptions?
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Because, exceptions can be encountered at either compile time or run time.
1. some exceptions can be predicted i.e if you are going to read a file, there is every possibility that either file is not readable, you don't have privilege to read it, it doesn't exist etc. that's why this is checked or this code can be detected at run time that this exception can come.
2. If exception is dependent on your input, a program doesn't know if your app has some validation and you will never pass 5/0 to your api. So this remains unchecked unless you will give bad input.

All exceptions subclass of Exception is checked and RuntimeException is unchecked.

I think this will help you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

harshvardhan ojha wrote:Because, exceptions can be encountered at either compile time or run time.

No. Exceptions are always thrown at runtime, never at compile time. The fact that RuntimeException has that name does not have anything to do with runtime versus compile time.

The page The Catch or Specify Requirement from Oracle's Java Tutorials explains what the difference is between checked and unchecked exceptions and when what type is supposed to be used.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Thanks Jesper , my bad, i mean which be determined at compile time or run time.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The difference between checked and unchecked exceptions also does not have anything to do with what can or cannot be determined at compile time or at runtime.

The reason why for example FileNotFoundException is a checked exception and not an unchecked exception is actually just a choice that was made by somebody who invented that exception long ago. There's no real technical reason why it could only be a checked exception.

The page that I linked to explains what the philosophy is. Checked exceptions are supposed to be used for conditions that the programmer can reasonably expect to happen, such as a file that can't be found. Unchecked exceptions are supposed to be used for conditions that should never happen, and that mean that there is probably a bug in the application.

Note that in practice, not everybody follows this philosophy. In particular, there are people who think that checked exceptions are unnecessary or even a bad idea, and that you should use unchecked exceptions for everything. Oracle's tutorial has some more information on that on the page Unchecked Exceptions — The Controversy.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
harshvardhan ojha wrote:Because, exceptions can be encountered at either compile time or run time. . . . .
Nonsense. Exception only occur at runtime. “Determined at compile time or run time” is confusing, and probably wrong, too.

I suggest you start with the Java Tutorials. There are not two kinds of Exception; there are at least three. Checked Exceptions, unchecked Exceptions and Errors.
Errors are things which can go wrong with the JVM for which there is likely to be no chance of recovery. At least in theory.
Runtime Exception and its subclasses are things which go wrong entirely inside the runtime, and are regarded as being caused by mistakes in the code, which cannot be recovered by trying again. There are some classes which do not fulfil that description, e.g. NumberFormatException. To add to what Jesper said, RuntimeException is called that because it is something which happens entirely inside the runtime. Other Exceptions may occur at an interface between the runtime and other parts of the computer/network, or outside the runtime.
Checked Exceptions can in theory be recovered from by supplying more information and trying again. At least in theory.

You will find this is one of the features of Java which has engendered the most controversy.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

harshvardhan ojha wrote:Thanks Jesper , my bad, i mean which be determined at compile time or run time.


No, that's still not right. That statement only reiterates, sort of, what the two types are. It doesn't answer the "why" question.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote: There are not two kinds of Exception; there are at least three. Checked Exceptions, unchecked Exceptions and Errors.


While that's true with the capitalization, that's just a special case of there being two types of exception (lower case "e")--checked and unchecked. From JLS (JSE 5 & 6) 11.2 Compile-Time Checking of Exceptions: "The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. ." Section 11.2.4 goes on to include Error in "unchecked exceptions."

The Java 7 JLS words it a bit differently, but still says the same thing.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7553
    
  18

Jesper de Jong wrote:In particular, there are people who think that checked exceptions are unnecessary or even a bad idea, and that you should use unchecked exceptions for everything.

And then, there are ornery types like me who think that having both types is good, but that the choices of making
(a) the type strictly hierarchical,
and
(b) certain vague "catch-all" exceptions, like IOException and SQLException checked,
were bad ones.

It's too late now, but personally I'd have made CheckedException a marker interface...

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
It all depends what you mean by three. Agree with WG that a marker interface would have been a good idea.
 
Consider Paul's rocket mass heater.
 
subject: why there are exceptions of 2 types?
 
Similar Threads
Exception
Checked and JVM Exceptins
Use of Checked and Unchecked Exception
exception types
why to remember names of specific exceptions??