[研究][ASP.NET]Exception ex, ex.Message, ex.Message.ToString() 比較
2022-08-10
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
********************************************************************************
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication17.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </form> </body> </html> |
Default.aspx.cs
using System; namespace WebApplication17 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Int32.TryParse(TextBox1.Text, out int a); Int32.TryParse(TextBox2.Text, out int b); Label1.Text = (a / b).ToString(); } } } |
(下圖)詳細除錯資訊
********************************************************************************
加上 try catch 後
Default.aspx.cs
using System; namespace WebApplication17 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { Int32.TryParse(TextBox1.Text, out int a); Int32.TryParse(TextBox2.Text, out int b); Label1.Text = (a / b).ToString(); } catch (Exception ex) { var c1 = ex; var c2 = ex.Message; var c3 = ex.Message.ToString(); Label1.Text = ex.Message; var c4 = "test"; } } } } |
(下圖) ex 是 Exception 類型的物件
沒有留言:
張貼留言