Page 1 of 1

[HD Pg 293, Sec. 10.4.0 - common-errors-usually-thrown-by-the-jvmerrors-thrown-by-jvm]

Posted: Sat Mar 16, 2019 7:45 pm
by OCAJO1
public void processData(byte[] data, int datatype)
{
if(datatype != 1 || datatype != 2)
throw new IllegalArgumentException("Invalid datatype "+datatype);
else System.out.println("Data Processed.");
}
Throwing this exception in place of a simple System.out.println("Invalid datatype "+datatype); will hand over the program flow to either calling method's try/catch block or if no try/catch block, to the JVM.

Question: Without knowing if the calling method has the "means" to handle such exception, why would we write a code that could cause JVM to halt a (the) program?

Thanks

Re: [HD Pg 293, Sec. 10.4.0 - common-errors-usually-thrown-by-the-jvmerrors-thrown-by-jvm]

Posted: Sat Mar 16, 2019 8:15 pm
by admin
processData method shouldn't care about what the calling method is capable of. The calling method has to care about what processData has to offer, how it works, and what exception it might throw in what situation.
You would write code based on what service you want to provide through that method.