• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why there are exceptions of 2 types?

 
Guna sekar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there are exceptions of two types - checked vs unchecked exceptions?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper , my bad, i mean which be determined at compile time or run time.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends what you mean by three. Agree with WG that a marker interface would have been a good idea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic