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.
Joined: May 22, 2007
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.
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 ]
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.
Joined: May 22, 2007
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...