I have some code which ignores a specific exception.
try
{
foreach (FileInfo fi in di.GetFiles())
{
collection.Add(fi.Name);
}
foreach (DirectoryInfo d in di.GetDirectories())
{
populateItems(collection, d);
}
}
catch (UnauthorizedAccessException ex)
{
//ignore and move onto next directory
}
of course this results in a compile time warning as ex is unused. Is there some standard accept noop which should be used to remove this warning?
When the exception happens and the catch block above is executed, nothing will be done by Java. The exception is ignored and the next line of code below the catch block will be executed as if nothing has happened.
To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block. Let's see an example of how to do this. In your main() method, you can surround the call to the checkAge() method with a try...
if you don't handle exceptions When an exception occurred, if you don't handle it, the program terminates abruptly and the code past the line that caused the exception will not get executed.
There are three types of exception—the checked exception, the error and the runtime exception.
Just rewrite it as
catch (UnauthorizedAccessException) {}
As Dave M. and tvanfosson said, you want to rewrite it as
catch (UnauthorizedAccessException) {}
The bigger question that should be asked, however, is why you are catching an exception on ignoring it (commonly called swallowing the exception)? This is generally a bad idea as it can (and usually does) hide problems in the application at runtime that can lead to very strange results and a difficult time debugging them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With