• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Confusion between checked and unchecked exceptions in java

 
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading Head First Java 2 edition. In chapter 11 (Exception handling in java), on page 324, book says that checked exceptions are:-
1) Except runtime exceptions, all other are checked exceptios,
2) Checked exception are checked by the compiler like 'filenotfoundexception', etc where we are not sure, gurantee that something will be there at some specific location or unpredicatable ,etc (means not flaw in code).

But I'm confused with unchecked exception, on same page(324), book says unchecked exceptions are not checked by compiler like your nullpointerexception or typecastexception, etc.

Please clear my following doubts:-
1)As book says, we can do exception handling(using try-catch blocks) for unchecked exceptions but the compiler will not check. Does that mean a program pre-mature death or execution termination if such things happen like unchecked(runtime exceptions -> nullpointerexception, etc) ?

2) Now I think there is a contradiction on page 348, there is an exercise to do (True/False) for us. Point# 4 asks "Only 'compiler checked' exceptions can be caught". I said Yes as till now I was thinking like this only. But my answers was wrong. On page 351, solutions are given and now book says False, runtime exceptions can be caught.

What does it mean? Please tell me which one is correct.
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if your code is referring to an index that does not have any element on your array, your code will compile, but the program will crash at runtime.
However, you can catch all kinds of exceptions (using a try-catch construct) in your code.
The checked exceptions MUST be caught as they cannot be compiled without handling them. You can use throws clause in your method if you do not want to catch it locally, but then too, the caller of that code is responsible for handling the exception.
For unchecked exception, there is no such necessity but that does not means that the exception will not take place if the necessary conditions are met.
 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked Exception you can think of it as a kind of Customized Exception we are creating

Unchecked Exception you can think about some exception that occurs during the compilation of the program and is caught only by runtime Inspectors ;)
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:Please clear my following doubts:-
1)As book says, we can do exception handling(using try-catch blocks) for unchecked exceptions but the compiler will not check. Does that mean a program pre-mature death or execution termination if such things happen like unchecked(runtime exceptions -> nullpointerexception, etc) ?

2) Now I think there is a contradiction on page 348, there is an exercise to do (True/False) for us. Point# 4 asks "Only 'compiler checked' exceptions can be caught". I said Yes as till now I was thinking like this only. But my answers was wrong. On page 351, solutions are given and now book says False, runtime exceptions can be caught.

What does it mean? Please tell me which one is correct.



Please explain my two queries in detail with some example. Also about True/False exercise.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:

Vinod Vijay wrote:Please clear my following doubts:-
1)As book says, we can do exception handling(using try-catch blocks) for unchecked exceptions but the compiler will not check. Does that mean a program pre-mature death or execution termination if such things happen like unchecked(runtime exceptions -> nullpointerexception, etc) ?

2) Now I think there is a contradiction on page 348, there is an exercise to do (True/False) for us. Point# 4 asks "Only 'compiler checked' exceptions can be caught". I said Yes as till now I was thinking like this only. But my answers was wrong. On page 351, solutions are given and now book says False, runtime exceptions can be caught.

What does it mean? Please tell me which one is correct.



Please explain my two queries in detail with some example. Also about True/False exercise.




I will answer your 2nd question : you can catch checked as well as unchecked exception , but its mandatory that you catch a checked exception.


The first question is confusing to me what actually do you mean by pre-mature death???
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:Unchecked Exception you can think about some exception that occurs during the compilation of the program...


Please note that Exceptions occur during the execution of the program
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:

Vishal Hegde wrote:Unchecked Exception you can think about some exception that occurs during the compilation of the program...


Please note that Exceptions occur during the execution of the program



Oops rite it compiles but during execution it creates a runtime exception
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:

I will answer your 2nd question : you can catch checked as well as unchecked exception , but its mandatory that you catch a checked exception.


The first question is confusing to me what actually do you mean by pre-mature death???



By pre-mature death, I mean to say before the program encounters closing '}' braces, execution gets teriminates in between.
So you mean to say, we can catch both checked and unchecked exceptions but it is mandatory to catch checked expection by writing it using try-catch block. And there is no pre-mature death(execution termination) of program for checked exceptions?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Runtime Exceptions are called as Unchecked Exceptions. Error and its subclasses also throw Unchecked Exceptions.

NullPointerException is an unchecked Runtime Exception. OutOfMemory is Error Unchecked Exception.

Example of NullPointer Exception:

String s;
System.out.prinlnt(s.toUpperCase()) will throw null pointer Exception as s is not assigned to any object and it contains null and we are trying to print s.toUpperCase() and it compiles but fails at runtime.

If still any queries regarding exceptions let me know.
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Surya Narayana Murthy wrote:Hello,

Runtime Exceptions are called as Unchecked Exceptions. Error and its subclasses also throw Unchecked Exceptions.

NullPointerException is an unchecked Runtime Exception. OutOfMemory is Error Unchecked Exception.

Example of NullPointer Exception:

String s;
System.out.prinlnt(s.toUpperCase()) will throw null pointer Exception as s is not assigned to any object and it contains null and we are trying to print s.toUpperCase() and it compiles but fails at runtime.

If still any queries regarding exceptions let me know.



Thanks Surya, so it means the only difference between checked and unchecked is that for Checked, the compiler forces us to use try-catch block(Yes, now I can recall when I try to serialize an object in main() without using try-catch block, compilation error I get on eclipse) because its a checked exception and compiler is checking whether we have done exception handling or not whereas incase of Unchecked(runtime exception), compiler does not check whether we have done exception handling or not. And If we do exception handling for unchecked exception then it is good and [b]will be caught else will not (and execution terminates).

Please confirm my understanding.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

forces us to use try-catch block


You also have the alternative throws declaration.

And If we do exception handling for unchecked exception then it is good and [b]will be caught else will not (and execution terminates).


Your understanding is correct. But handling RuntimeException in catch block is not a good standard. You can either allow it to propagate or check in code.
In case of Surya's example, I would check the value of s before calling any method on it.



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:Checked Exception you can think of it as a kind of Customized Exception we are creating



We can create our own custom checked and unchecked exceptions, and the exceptions in the core API are both checked and unchecked, so that's not a particularly useful way to view them.

Unchecked Exception you can think about some exception that occurs during the compilation of the program and is caught only by runtime Inspectors ;)



No, that would be wrong. Exceptions don't occur during compile time.

The right way to think about them is the way they're explained in the docs and tutorials: Checked exceptions are for things that are expected to go wrong and that we have a good chance of recovering from, and unchecked exceptions are those that indicate either a bug in the code or a serious problem in the JVM, and we generally cannot recover from them, and generally should not try to.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:but its mandatory that you catch a checked exception.



No it's not. You can also declare that you throw it.
 
Marshal
Posts: 80617
469
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote: . . . Does that mean a program pre-mature death or execution termination . . .

It’s called abrupt completion. You should read that whole Java™ Language Specification chapter (it’s not easy to read), as well as the section in the Java Tutorials.
 
Suryanarayana Murthy Maganti
Greenhorn
Posts: 17
Hibernate Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya What john told is right. What you told is also right as you can use try-catch method or throws clause. Any questions or suggestions or improvements are always welcome
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Vishal Hegde wrote:Checked Exception you can think of it as a kind of Customized Exception we are creating



We can create our own custom checked and unchecked exceptions, and the exceptions in the core API are both checked and unchecked, so that's not a particularly useful way to view them.

Unchecked Exception you can think about some exception that occurs during the compilation of the program and is caught only by runtime Inspectors ;)



No, that would be wrong. Exceptions don't occur during compile time.

The right way to think about them is the way they're explained in the docs and tutorials: Checked exceptions are for things that are expected to go wrong and that we have a good chance of recovering from, and unchecked exceptions are those that indicate either a bug in the code or a serious problem in the JVM, and we generally cannot recover from them, and generally should not try to.




Dear Jeff,

How Unchecked Exception can be customised??
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vishal - pardon me. Am not Jeff

By customizing if you mean writing your custom classes to act as Unchecked Exception then you can extend RuntimeException.

An example below - you should note that no error is thrown if MyRuntimeException is not handled.

 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear John,

Thanks a lot
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:
How Unchecked Exception can be customised??



Exactly like checked exceptions. If you extend a checked exception, your new class is also a checked exception. If you extend an unchecked exception, your new class is also an unchecked exception.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic