《C#入门详解》刘老师 表达式,语句详解
class program { static void main(string[] args) { calculator c = new calculator(); int r = 0; try { r = c.add("abc","100"); } catch(overflowException oe) { console.writeline(oe.message); } } } class Calculator { public int Add(string arg1 , string arg2) { int a = 0; int b = 0; try { a = int.parse(arg1); b = int.parse(arg2); } catch(ArgumentNullException) {console.writeling("your arguments are null");} catch(FormatException) {console.writeling("your arguments are not number");} catch(OverFlowException oe) { //console.writeline("out of range"); throw oe;//只希望处理上面的两个异常,把overflow这个异常抛出去,意思是谁调用 这个add方法,谁去抓住这个异常进行处理 } /*可以在catch后面的圆括号内加上标识符( catch(ArgumentNullException ane)),可以打印出具体错误消息。*/ /*try catch finally,当执行try语句的时候,无论是否发生异常,finally语句永远会执行,finally语句中一般会写两类内容,第一类数据库链接总能关闭,第二类写程序的log*/ } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步