Reputation: 33243
A very noob question. I am writing a method which does takes in two arrays and then creates a hash map out of it. So for example:
HashMap<String,String> createHash(String[] arr1, String[] arr2){
//the logic is very simple
HashMap<String,String> hshmp= new HashMap<String,String>();
for (int i=0; i < arr1.length;i++){
hshmp.put(arr1[i],arr2[i]);
}
return hshmp
}
Now first thing I want to check is dimensions of both arr1 and arr2..whether they are same or not
and if not.. then the program quits (gracefully ?? elaborate??)
Usually this is assertion??? right?? but i think assertion in java is optional thingy which you have to enable?
Whats a good way to solve this?
Upvotes: 1
Views: 135
Reputation: 13941
The choice between exceptions and assertions depends on some extent to how the method is intended to be used.
Assertions are intended to verify internal assumptions - if you have control over all calls to createHash
and, because of the logic of your code, it should be impossible for the method to be called with different-length inputs, then an assertion might be appropriate. The thought is, you would enable assertions during development to verify your assumptions, and then disable them in production for performance reasons.
For methods that might accept user input that you have no control over, or be part of a public API, or a library that's used internally by lots of developers, then an exception would be more appropriate, because an exception is guaranteed to be thrown. In public methods, you need to be more proactive about your checking your inputs, and throwing exceptions is typically how that's done.
Upvotes: 3
Reputation: 68847
An IllegalArgumentException is perfect for that purpose:
if (arr1.length != arr2.length)
{
throw new IllegalArgumentException("Arrays do not have the same length: " +
arr1.length + " != " + arr2.length);
}
I recommend throwing an exception, because this way, the caller of your method can decide what to do when that happens. The caller can exit the application by putting a System.exit(-1);
statement or calling your custom terminate procedure (where you save changes, resources, settings, etc) in the catch
body for IllegalArgumentException.
FYI: Yes, assert is a feature you have to enable manually.
Alternatively, you could print out the stacktrace of the error and exit immediately. But I do not recommend:
if (arr1.length != arr2.length)
{
Exception e = new IllegalArgumentException("Arrays do not have the same length: " + arr1.length + " != " + arr2.length);
e.printStackTrace();
System.exit(-1);
}
Upvotes: 2
Reputation: 1774
you can do it like this :
HashMap<String,String> createHash(String[] arr1, String[] arr2) throws IllegalArgumentException {
//the logic is very simple
if(arr1 != null && arr2 !=null && arr1.length == arr2.length){
HashMap<String,String> hshmp= new HashMap<String,String>();
for (int i=0; i < arr1.length;i++){
hshmp.put(arr1[i],arr2[i]);
}
return hshmp;
}
else
throw new IllegalArgumentException("Arrays are null or not equally sized");
}
Upvotes: 1