File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Diff b/w checked & unchecked  exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Diff b/w checked & unchecked  exceptions" Watch "Diff b/w checked & unchecked  exceptions" New topic
Author

Diff b/w checked & unchecked exceptions

chaitanya gopal
Greenhorn

Joined: May 22, 2007
Posts: 13
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..?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: May 22, 2007
Posts: 13
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.
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
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.


Gunjan Kumar
SCJP 1.5
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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 ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    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 ]
    Pravin Jain
    Ranch Hand

    Joined: May 20, 2007
    Posts: 60
    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.


    The Zen of Java Programming.
    chaitanya gopal
    Greenhorn

    Joined: May 22, 2007
    Posts: 13
    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

    Joined: Apr 20, 2006
    Posts: 3344

    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?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Diff b/w checked & unchecked exceptions