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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: marpiva and 28 guests