Throw Simple Exception in Java -
i'm looking learn how throw super simple exception in java. have following:
public percolation(int n) // create n-by-n grid, sites blocked { if(n < 1) throw new exception("n must greater zero."); grid = new boolean[n * n + 2]; dimension = n; grid[0] = true; grid[n+1] = true; unionstruct = new quickfinduf(n+2); }
it's not compiling, that's type of thing i'm looking do. what's proper syntax this?
it's because you're throwing checked exception without declaring exception you're throwing. in case should throwing exception derived runtimeexception instead, , these not checked (meaning don't have declare them). 2 ways fix are
throw new illegalargumentexception("n must greater zero."); // unchecked
or
public percolation(int n) throws exception
Comments
Post a Comment