Z
zaemin
Hello everyone~
I have a question about the exception of java.
Actually, I knew how it works and how I can use it.
But what I wonder is 'when do I have to use it?'
I think that I can avoid all the situation that the exceptions occurs.
I can avoid the situation that the index of array passes the last
element or some value is divided by zero by checking the variable.
These are what I can do to prevent from zero-division as an example.
1. Avoid zero-division by check the value.
....
if(d != 0)
return a / d;
else
return -1; // Assume this return value indicates an error
simply.
....
2. Avoid zero-division by exception.
....
try{
return a / d;
}catch{ArithmeticException e){
return -1; // Assume this return value indicates an error
simply.
}
...
In java, which one is better?
If there is no exact answer for this kind of situation,
Can anyone give me guides to distinguish when to use 'code checking'
and when to use 'exception'?
Thanks for your considerations.
I have a question about the exception of java.
Actually, I knew how it works and how I can use it.
But what I wonder is 'when do I have to use it?'
I think that I can avoid all the situation that the exceptions occurs.
I can avoid the situation that the index of array passes the last
element or some value is divided by zero by checking the variable.
These are what I can do to prevent from zero-division as an example.
1. Avoid zero-division by check the value.
....
if(d != 0)
return a / d;
else
return -1; // Assume this return value indicates an error
simply.
....
2. Avoid zero-division by exception.
....
try{
return a / d;
}catch{ArithmeticException e){
return -1; // Assume this return value indicates an error
simply.
}
...
In java, which one is better?
If there is no exact answer for this kind of situation,
Can anyone give me guides to distinguish when to use 'code checking'
and when to use 'exception'?
Thanks for your considerations.