米琪卡哇伊
2022-04-21 15:11:03
try{smallfoldername = Server.MapPath(ConfigurationManager.AppSettings["SmallPath"]);bigfoldername = Server.MapPath(ConfigurationManager.AppSettings["NormalPath"]);}catch{smallfoldername = Server.MapPath("./Photo/SmallPics/");bigfoldername = Server.MapPath("./Photo/NormalPics/");}
3 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
在try...catch塊中,設定了兩個變量,變量需要讀取配置文件中兩個鍵的值,當讀取發生錯誤的時候就在catch中將兩個變量設默認值。
這樣是可以的,有時候在catch中出了要把錯誤寫出來之外,也要寫一些以防程序崩潰的代碼,就像這個代碼,如果不賦這兩個值也許在程序后方會讓程序崩潰。

DIEA
TA貢獻1820條經驗 獲得超2個贊
這么寫根本就不是try catch的正常用法.僅僅目前的功能完全可以去掉
可以用
smallfoldername = Server.MapPath(ConfigurationManager.AppSettings["SmallPath"]);
if(String.IsNullOrEmpty(smallfoldername))
smallfoldername = Server.MapPath("./Photo/SmallPics/");
bigfoldername = Server.MapPath(ConfigurationManager.AppSettings["NormalPath"]);
if(String.IsNullOrEmpty(bigfoldername))
bigfoldername = Server.MapPath("./Photo/NormalPics/");
添加回答
舉報
0/150
提交
取消