• 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:

Diff b/w checked & unchecked exceptions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I understand the unchecked exception, which are subclasses of RTE.
and we need not to maintain them. JVM will take care of them.
Am I correct and also anybody tell me abt checked exception?

and difference b/w throw and throws clause..?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chaitanya:
Hi friends,

I understand the unchecked exception, which are subclasses of RTE.
and we need not to maintain them. JVM will take care of them.
Am I correct and also anybody tell me abt checked exception?

and difference b/w throw and throws clause..?



If a method contains code that could potentially cause a checked exception to occur, then either the programmer must write their own exception handler with a try/catch block for that code, or they must list that exception in the throws clause of the method.

This is not required if the code could potentially only cause unchecked exceptions to occur.

There is a great deal of difference between throw and throws.

throws precedes a list of exceptions that the programmer explicitly states that a method might throw.

throw causes an exception to occur.
 
chaitanya gopal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lynn, I read no.of books for this topic: Diff b/w checked and unchecked exceptions... but i didn't get the clear idea if you provide example for this,that's so helpful for me....
thanks,
chaitanya.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most RuntimeException come from a problem in code logic, rather than a condition that falls at runtime in ways that one cannot predict or prevent.

So the compiler checks for everything except runtimeException. we can throw ,catch or declare runtime exception , but we dont have to , and compiler wont check.

In case of checked exception, we have to handle it(try/catch) or declare it(by ducking with trrows keyword), because compiler checks for it.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chaitanya,

Let me tell you this way.

Checked or Compile-time Exceptions:

Checked Exceptions are kinda exceptions which the compiler is insisted to check during compilation as these exceptions are present in the throws clause of the method declaration syntax.

Say, for example you are doing any file related operations, definitely the operations are not always assured to be successful because either the file itself might not be present, or there might be any issues with the file attributes like permissions etc which may cause the disturbance to the operations on the file. Thats why the methods on File or related classes will have the corresponding exceptions appended with the "throws" clause as they are tentatively thrown. Some exceptions are like FileNotFoundException,EOFException etc. They are all the child class exceptions of IOException which are in java.lang package.

By any chance, the user/developer makes use of the API (methods) for dealing with file but without handling the exceptions, the compiler does not know what to do when such exception occurs. It can't inturn take the responsibility of handling it. Thereby it always expects and insists the user to be aware of these exceptions and ways to handle them. Probably he can abort the program, or retry with a custom message, call someother method during exception etc.

The user can handle the exceptions in two ways.

  • by surrounding the statements which contains the methods-may-throw-exceptions in a try-catch block
  • by appending the probable exception classes in throws clause of the calling method



  • Thats why they are called *checked* or *compile-time* exceptions as the compiler is responsible for *checking* it during *compile-time*.

    Unchecked or Runtime Exceptions:

    You may ask, it more or less seems to be of runtime exceptions. But this has got nothing to do with the time when the exceptions are thrown. Because, certainly all exceptions are thrown at runtime only. As the term exception is defined, its an error preventing the running flow of an application. Right?

    But the Runtime Exceptions are of different category, wherein the user is not insisted to be aware of the exceptions which may likely to be thrown at runtime thereby no need to handle them in the code.

    Say for example, when you are dealing with an Array and the array is being accessed in a loop or with the value passed in command line argument for the index. In that case, the value might exceed by either case while accessing the array thereby an ArrayIndexOutOfBoundsException will be thrown. The user is not insisted to handle this and the compiler is also not asked to check for these kind of exceptions being handled in the code during compilation.

    One other famous example of Runtime Exception is NullPointerException which is being thrown when there is an invocation on the null object reference.

    Hope this helps to certain extent.
    [ May 31, 2007: Message edited by: Raghavan Muthu ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    throw Vs throws:

  • throw is a keyword used to throw an exception explicitly. It is an executable statement which can be present anywhere inside a method
  • throws is a keyword used to indicate that the method will be throwing or can throw the exceptions listed. its present in the syntax of the method



  • HtH.
    [ May 31, 2007: Message edited by: Raghavan Muthu ]
     
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Difference between throw and throws
    throw is a statement use to indicate an error and ir used
    to indicate the failure of the method. ie. the method is
    not successful and will not be returning a value as
    indicated in the method signature. whereas a throws is
    a part of the method signature used to indicate the kind
    of errors (failures) which may occur in this method.
    ie. the method header indicates both the return type
    the kind of values the method can return in case it is
    successful and throws to indicate the kind of errors
    (Throwable) which may fail the method.

    The difference between checked and unchecked exceptions
    is that CheckedExceptions which are possible from a method
    are compulsorily to be indicated in the throws clause which
    is not the case with unchecked, for unchecked exceptions
    which may occur in a method it is not compulsory to mention
    in the list of throws.
     
    chaitanya gopal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi friends,

    Is it possible to throw an exception without declaring at method declaration using throws keyword..?
    I am asking about only exceptions[checked] not the [unchecked]runtime exceptions and the errors.

    I am confusing about throws and throw..which one used in which situations..?please let me know guys...

    thanks,
    chaitanya.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chaitainya,

    Please go through the detailed reply given by me and Pravin Jain. I hope you can understand if you read it patiently.

    Also why dont you just write a piece of code which throws an error but not having it in the throws clause and test it?
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic