try catch - Two different ways of "catching" two different kinds of exceptions in C#. Is one better than another? -
i've never had need catch more 1 exception @ time before. here's scenario. want "try" copy file. if destination doesn't exist, don't want bothered it. still want catch other type of exception. unauthorizedaccessexception example. below 2 things i've tried, i've seen both examples used on web. 1 better coding another. or completly wrong on both? thanks.
catch (directorynotfoundexception) { // nothing } catch (exception ex) { messagebox.show(ex.message); }
or
catch (exception ex) { if (ex directorynotfoundexception) { // nothing return; } else { messagebox.show(ex.message); } }
imho, having multiple catch reduce code-readability. suggest.
if (directory.exists(dirpath)) { try { file.copy(sourcefile, destfile); } catch (exception msg) { //handle exception. } }
Comments
Post a Comment