new Exception()
means you are creating a new instance of Exception type. Which means you are just instantiating an object similar to others like new String("abc")
. You would do this when you are about to throw an exception of type Exception
in next few lines of code execution.
While when you say throw new Exception()
this means you are saying move the program control to caller and don't execute the further statements after this throw statement.
You would do this in a situation where you find that there is no way to move ahead and execute further and hence let caller know that i can't handle this case and if you know how to handle this case, please do so.
There is no best practice as such as you are comparing oranges with apples. But remember when throwing an exception, you always throw a meaningful exception like IO has where if file is not present it throws FileNotFoundException
instead of its parent IOException
.
new Exception()
does not throw the instantiated exception. – Lavella