exception that the program has to handle with and to be able to successfully recover from.
is checked by the compiler at compile time. It’s mandatory for a method to either handle the checked exception or declare them in their throws clause. These are the ones which are a sub class of Exception but doesn’t descend from RuntimeException.
Unchecked (Runtime) –
usually signal of a programmatic error. Potentially can happen anywhere.
The unchecked exception is the descendant of RuntimeException and not checked by the compiler at compile time.
Error – something very bad happened to the JVM or system. Usually cannot be recovered from.
5. If I want an object of my class to be thrown as an exception object, what should I do? ⤴
Extend from Exception class (or any of its subclasses)
6. If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object? ⤴
Nothing. There is no Exception interface.
7. How does an exception permeate through the code? ⤴
It’s either caught or it is going to be passed to the next level in call stack.
If it’s caught it can be rethrown as another Exception which has to be processed in a higher level.
8. What are the different ways to handle exceptions? ⤴
Declare that the methods throws an exception or catch it.
9. What is the basic difference between the 2 approaches to exception handling…1> try catch block and 2> specifying the candidate exceptions in the throws clause? ⤴
The main difference is in who is responsible for handling the exception.
As I understand one should handle the exception if he/she has all the knowledge and resources to do so. And the exception has to be handled as soon as this knowledge is acquired.
However if you are writing a library you may consider declaring the exceptions that are thrown and force the user of the library to decide how to react to a specific situation. The downside of this approach is that declared exceptions become the part of the API which has to be frozen after being published.
11. Is it necessary that each try block must be followed by a catch block? ⤴
No. It can be followed by either catch block or finally block or both.
12. If I write return at the end of the try block, will the finally block still execute? ⤴
Yes it will.
13. If I write System.exit (0); at the end of the try block, will the finally block still execute? ⤴
No it won’t. It also won’t execute if the thread where the exception is thrown is terminated.
14. How does a try statement determine which catch clause should be used to handle an exception? ⤴
The first that has a matching exception type declared.
15. What is the catch or declare rule for method declarations? ⤴
blah-blah-blah. If you are using a checked exception you either have to catch it or declare that it could be thrown by a method.
16. Pros and cons of using exceptions vs. returning “error value” ⤴
Exceptions provide clearer understanding and better code
Exceptions can be grouped together to be handled
Exceptions can be propagated up in the call stack to be handled where they are supposed to be handled.
Exception handling could be slow.
In some sense Exceptions are unignorable error codes.
For example, exceptions are intrusive (eg if they’re not handled the program dies). If you have an error that can be safely ignored, being forced to catch and handle can be painful. However, they’re definitely appropriate if the result of quietly ignoring an error is something that will bite you later.
Declare that method throws the exception. You can still use try/finally.
21. The difference between throw and throws in Java? ⤴
the throw is used to actually throw an instance of java.lang.Throwable class, which means you can throw both Error and Exception using throw keyword e.g.
thrownewIllegalArgumentException("size must be multiple of 2");
On the other hand, throws is used as part of method declaration and signals which kind of exceptions are thrown by this method so that its caller can handle them. It’s mandatory to declare any unhandled checked exception in throws clause in Java.
So,
throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly.
If we see syntax, throw is followed by an instance variable and throws is followed by exception class names.
The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).
By using throw keyword in java, you cannot throw more than one exception but using throws you can declare multiple exceptions.
Leave a Comment