Try catch exception handling mechanism in ASP.

Source: Internet
Author: User
Tags finally block pdfinfo try catch umbraco

In the process of developing a Umbraco platform system, problems have been encountered. Write the code as follows

FILEURL = Mediahelper.getmediaurl (Convert.ToInt32 (Publishedcontent.getproperty ("mediapdf" ). Datavalue.tostring ()));

This is in the background of Umbraco built a pdfinfo DocumentType, it has a property is Mediapicker, from the Media folder to choose PDF files. This property name is Mediapdf, here is the Mediapdf property value (Node ID), converted to Int32 type, and then to get this PDF in the media folder

Under the URL.

Now there is a problem, the content editor in the Umbraco background, add a pdfinfo type of DocumentType, forget to add the Mediapdf attribute, that is, do not select a file from the media folder, then this property value is a null value, Convert to Int32 type Convert.ToInt32 will have an error.

How to solve it? Write the following statement

  var "" ;                 Try                 {                    = Mediahelper.getmediaurl (Convert.ToInt32 (Publishedcontent.getproperty ("  Mediapdf"). Datavalue.tostring ()));                }                 Catch (Exception e)                {                }

Here is the use of the Try Catch statement block

The catch statement is used in the following way:

catch (Exception e)

{ }

In any case, the compilation will be

The following is an understanding of the try Catch statement block

Usually, when we are programming, we place the section of code that might throw an exception in a Try block, and the code that handles the exception is placed in a Catch block. A catch block is a series of statements that begin with a keyword catch, followed by the exception type and the action to be performed.

Here is a detailed explanation of the use of Try catch:

Reprinted from http://blog.csdn.net/studyofnet/article/details/13083999

Overview of Exceptions:

1. An exception occurs when an application encounters an exception, such as a 0 or out-of-memory warning.

2. Use a try block around the statement that could throw an exception.

3. After an exception occurs in a try block, the control flow immediately jumps to the associated exception handler, if it exists.

4. If there is no exception handler for the given exception, the program stops executing and displays an error message.

5. If a catch block defines an exception variable, you can use it to get more information about the type of exception that occurred.

6. Operations that may cause an exception are performed through the Try keyword.

7. An exception handler is a block of code that executes when an exception occurs. In C #, the Catch keyword is used to define an exception handler.

8, the program can use the Throw keyword to explicitly throw an exception.

9. The exception object contains detailed information about the error, such as the status of the call stack and a textual description of the error.

10. Even if an exception is thrown, the code in the finally block executes, allowing the program to release resources.

Exception General usage:
Try  {   //program code block;//try to execute the code block and catch the exceptions that may occur}  Catch(Exception e) {//Exception handling code block, if the exception is not handled, the program will be aborted. //a try, which can correspond to multiple catch, the exception handling mechanism will search for the parameter and the exception type, enter the catch clause. Catch clauses that catch exceptions must be correctly arranged, with a small range of exception placed in the preceding catch.     That is, if there is an inheritance relationship between the exception, the exception of the class should be placed in the preceding catch clause. //If the exception is not currently being processed, throw it again. You can also throw an exception here to implement the encapsulation of the error;//A throw (throw;) without an expression will again throw the exception caught by the catch. //with a catch clause without parameters, you can catch any type of exception}  finally  {  //exists outside of the standard event stream for exception handling. //other actions to clean up resources or restore related settings information//the code block to execute if an exception occurs;}
Exception Handling Usage recommendations:

1. Do not return the error code. Exceptions are the primary means of reporting errors in the framework.

2. Do not use exceptions for normal flow of control as much as possible. In addition to system failures and actions that can cause contention, the framework designer should design some APIs so that users can write code that does not throw exceptions. For example, you can provide a way to check a precondition before invoking a member so that the user can write code that does not throw an exception.

3. Do not include public members that can throw or throw exceptions based on an option.

4. Do not include a public member that returns an exception as a return value or an output parameter.

5, consider using the exception generator method. Throwing the same exception from a different location often occurs. To avoid code bloat, use a helper method to create an exception and initialize its properties.

6. Avoid explicitly throwing an exception from a finally block. You can accept an exception that is implicitly thrown by calling the method that throws the exception.

Try catch exception handling mechanism in ASP.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.