当数组为 null 或为空时如何抛出 IllegalArgumentException?
How to throw an IllegalArgumentException when an array is null or empty?
/**
* This constructor accepts an array of points as input. Copy the points into the array points[].
*
* @param pts input array of points
* @throws IllegalArgumentException if pts == null or pts.length == 0.
*/
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
try{
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}
catch()
{
}
}
我知道这应该很简单,但是在这些情况下我该如何抛出这个异常?
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length ==0 )
throw new IllegalArgumentException();
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}
/**
* This constructor accepts an array of points as input. Copy the points into the array points[].
*
* @param pts input array of points
* @throws IllegalArgumentException if pts == null or pts.length == 0.
*/
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length == 0)
throw new IllegalArgumentException();
else{
for(int i = 0; i < pts.length; i++)
points[i] = pts[i];
}
}
这是我根据您的建议得出的。
你的代码应该是这样的,
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length == 0 ){
throw new IllegalArgumentException();
}
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}
/**
* This constructor accepts an array of points as input. Copy the points into the array points[].
*
* @param pts input array of points
* @throws IllegalArgumentException if pts == null or pts.length == 0.
*/
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
try{
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}
catch()
{
}
}
我知道这应该很简单,但是在这些情况下我该如何抛出这个异常?
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length ==0 )
throw new IllegalArgumentException();
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}
/**
* This constructor accepts an array of points as input. Copy the points into the array points[].
*
* @param pts input array of points
* @throws IllegalArgumentException if pts == null or pts.length == 0.
*/
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length == 0)
throw new IllegalArgumentException();
else{
for(int i = 0; i < pts.length; i++)
points[i] = pts[i];
}
}
这是我根据您的建议得出的。
你的代码应该是这样的,
protected AbstractSorter(Point[] pts) throws IllegalArgumentException
{
if(pts == null || pts.length == 0 ){
throw new IllegalArgumentException();
}
for(int i = 0; i < pts.length; i++)
{
points[i] = pts[i];
}
}