---------------------------------------------------------------------------------------------------------------
Description -
Exception is a condition where a block of code behaves unexpectedly due to unexpected input from the user or due to programmer's irresponsible code.
---------------------------------------------------------------------------------------------------------------
Key Points-
1. One should handle checked exception in code (either in try/catch block or throws keyword).
Illustration -
2. Checked exception is good in some situations as compiler forces programmer to handle checked exception and hence application not breaks.
3. Throw your own exception from catch block when an exception is caught. This helps you to maintain discipline as you handle exceptions in one place.
Illustration -
4. Runtime exceptions should not be handled in catch block or thrown from a method. Because, these exceptions can be avoided through
programmer and client can't do anything from this exception. So, these are called unchecked exceptions.
Ex :- ArithmeticException, ArrayIndexOutOfBoundsException, IllegalArgumentException etc.
5. Use checked exceptions when user can change the input based on the message given by exception.
6. finally block should be used when you are dealing with resources. You have to close resources irrespective of exception.
------------------------------------------------------------------------------------------------------------------
Description -
Exception is a condition where a block of code behaves unexpectedly due to unexpected input from the user or due to programmer's irresponsible code.
---------------------------------------------------------------------------------------------------------------
Key Points-
1. One should handle checked exception in code (either in try/catch block or throws keyword).
Illustration -
main(){ // you can also declare main() using throws keyword main() throws InsufficientFundsException
B b = new B();
try{
b.myMethod(); // compiler forces you to handle exception
}catch(InsufficientFundsException ex){
ex.printstackTrace();
}
}
public class B{
public void myMethod() throws InsufficientFundsException{ // Custom exception defined by you. You can throw multiple exceptions.
if(balance <=0)
throw new InsufficientFundsException();
}
}
2. Checked exception is good in some situations as compiler forces programmer to handle checked exception and hence application not breaks.
3. Throw your own exception from catch block when an exception is caught. This helps you to maintain discipline as you handle exceptions in one place.
Illustration -
class A{
try{
myMethod();
}catch(BaseException ex){
ex.printstackTrace();
}
}
public class B{
public void myMethod() throws BaseException{ // Custom exception defined by you. You can throw multiple exceptions.
try{
// some database code
// application logic
}catch(SQLException ex){
ex.printstackTrace();
throw new BaseException("exception occurred while processing myMethod()");
}
}
}
4. Runtime exceptions should not be handled in catch block or thrown from a method. Because, these exceptions can be avoided through
programmer and client can't do anything from this exception. So, these are called unchecked exceptions.
Ex :- ArithmeticException, ArrayIndexOutOfBoundsException, IllegalArgumentException etc.
5. Use checked exceptions when user can change the input based on the message given by exception.
6. finally block should be used when you are dealing with resources. You have to close resources irrespective of exception.
------------------------------------------------------------------------------------------------------------------

No comments:
Post a Comment