Some tips for exception .......
TIP OF THE DAY
- For exception generally to make the class an exception class we write extends Exception.
- As we see we write extends exception here in the class we have throw catch and all usual process.
- for any exception we have to write its try and catch when we write extends exception as exception is the base class .
- But when we write extends RuntimeException then we didnt need to write try or catch block as its control when having an exception goes to the throwable class the base class of the exception class.
- With unchecked exceptions calling code method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
- As the calling method may not handle RuntimeException, one needs to be careful while throwing RuntimeException.
- Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.
- Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).
- If we write simply the throws keyword what it means that i couldnt handle the exception and will throw in future so be prepared if you want to use this ...
- And if we want to use we have to build its try catch
- let me take an example.
void calc throws ArithmaticException
{
// it declares that it will throw the exception
}
so its necessary for us to build its try catch
now as we know its calling statement will be from main
so as in java we have the block for main body say
Public static void main(string args[ ])
{
calc( );
}
- Here if it didnt find its catch block it will return from where it was called and was called from main but if catch block is found than exception will be caught.
- Think for a while that we have not kept a catch block so the control get transfer to main and here if we write besides main extends runtimeException than we will not get an erroe because main is called by the compiler and the exception is transfered to compiler so that exception is handled by the default handler .
- Normally in class also when we write extends runtimeException its not compulsory to write the catch block if we write than no issues .
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
- Here if it didnt find its catch block it will return from where it was called and was called from main but if catch block is found than exception will be caught.
- Think for a while that we have not kept a catch block so the control get transfer to main and here if we write besides main extends runtimeException than we will not get an erroe because main is called by the compiler and the exception is transfered to compiler so that exception is handled by the default handler .
- Normally in class also when we write extends runtimeException its not compulsory to write the catch block if we write than no issues .
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
Comments
Post a Comment