What is the best way to handle exceptions and how to deal with them in asp.net

First, I'm already familiar with the syntax of simple exception handling, but I'm asking about the best place, best time, and best way to deal with them.

I am creating an N-Layered application. so I think that DAL will someday create some errors for processing. And I just found out about the SqlException class, what is the deal with this class? I once saw code that handles SqlException, then it handles Exception!

Having learned about the practice and where I will handle it, I plan to create a method for connecting to the database and registering errors in the database so that I can fix it, but still I don’t know what information I need to collect so that I can determine the whole situation!


I thought exception handling was not a big deal. but from time to time I read some strange tips that I never understood - to questions, but no one could answer me, because these were very old questions!

"Don't just explicitly catch exceptions."

"code that uses higher levels in your application should always exclude exceptions only and never worry about how to deal with them."

EDIT

What about the event Page_Errorand Application_Error.. I saw that they are good practice for handling errors

+3
source share
5 answers

Exception handling is a big deal, and it’s not easy to develop a good strategy for this.

First of all, some general rules:

  • , , , , , . TCP: , , TCP . , I/O socket .
  • . , , . , ,
  • . , , , frist,

log , - . , .

"" . , , , . . , .

try-catch (), . .., FormatException, Object InvalidCastException

!! , , .. return null (, ANSI C) . . (.. , , , FileNotFoundException, , , ) , . , , .

, , ! , !

- . , .

. , , . - , . , , (a NullReferenceException ), , .. , !

ASP.NET

Page.OnError. , . log .

try-catch (Exception), , , catch, OnError.

, Server.RemoveError(). HTML- HTTP 500 ( , ASP.NET), .

  • throw , -
  • , , ( , , )
  • LOG!!!!!!!!!!!!!!!!!
  • -, ! ASP.NET , OnError
  • OnError Application_Error
  • / , , //.
+8

elmah. asp.net. .

http://code.google.com/p/elmah/

- , . , , 2 , .

-. , , .

SQL Connectivity DAL... ..

+1

, // , , , .


,

SqlException , ? - , SqlException !

, , , , , , "", , - "SQLException" - "", , , , .


" "

, ,

try 
{
   int i = 1/0;
} 
catch(Exception e)
{
  //do nothing
}

, , , , , , , .

+1

, / .

, :

- , , , , , , , , , , .....

. , . Application_Error(), . , , . DB- - , .

+1

IMO, except in extremely rare cases, I use only exception handling for code related to I / O, where there are interactions with services and file systems whose functionality and maintenance are beyond the control of my applications.

I have always considered using try / catch statements to manipulate logic (flow control) in a program in the same way if the if else statement is extremely bad practice. The most commonly used exceptions can be avoided if you use the tools correctly.

0
source

Source: https://habr.com/ru/post/1795392/


All Articles