C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (txtName.Text == "Jaya" && txtPwd.Text == "1234")
{
Server.Transfer("Retailer.aspx");
}
else
{
Server.Transfer("Login.aspx");
}
}
catch(Exception ex)
{
oExceptionManager.LogError(ex.Message, "Error From btn btnSubmit_Click in Login.aspx.cs");
}
}in my code the catch block is executing if it is true or false but i want to execute when i got false condtions pls modify my code

Naimish MakwanaRank #324 · 7.7K reputationPosted Oct 6, 2023, 5:33 AM EDT
In your code, the
tryandcatchblocks are correctly set up to catch exceptions that might occur within thetryblock. However, based on your requirement, it seems like you want to handle exceptions only when there's an error or issue with the code inside thetryblock. If you don't want to catch exceptions for business logic conditions like incorrect login credentials, you should modify your code as follows:protected void btnSubmit_Click(object sender, EventArgs e) { try { if (txtName.Text == "Jaya" && txtPwd.Text == "1234") { Server.Transfer("Retailer.aspx"); } else { // This is where you handle the case of incorrect login credentials // You don't need to catch an exception for this; instead, you can display a message to the user or log it as needed. ShowErrorMessage("Incorrect login credentials. Please try again."); } } catch (Exception ex) { // Handle any unexpected exceptions here oExceptionManager.LogError(ex.Message, "Error From btnSubmit_Click in Login.aspx.cs"); } }In the modified code, I've removed the
catchblock around the entiretryblock. Instead, I added a custom methodShowErrorMessageto handle the case of incorrect login credentials within theelseblock. This way, exceptions will only be caught and logged for unexpected issues, and you can handle business logic issues separately.Thanks
Naimish Makwana