Monorail study Note 9: Use of rescue

Source: Internet
Author: User

Rescue is a mechanism for handling exceptions in monorail.
In Asp.net or Java development, we need to handle exceptions, that is, when exceptions occur, jump to an error page. In the past, we used to define a method to handle exceptions automatically. In monorail, a ready-made mechanism is provided to handle exceptions.

The usage is simple. You only need to add a rescue attribute.
Let's take a look at the simplest way:

Controller Code : [Rescue ( " /Error " )]
Public   Class Errorcontroller: smartdispatchercontroller
{
Public Errorcontroller ()
{
}
[Rescue ( " /Argerror " , Typeof (Argumentexception)]
Public   Void Index ()
{
//Throw new argumentexception ("dddd ");
Throw NewException ("Fff");
}
}

VM code:< Html >
< Body >
< Center >
< H2 > Error </ H2 >
$ Context. lastexception. Message
</ Center >
</ Body >
</ Html >

When we call the/error/index. rails page, the/error. VM file is automatically displayed because the index method throws an exception, and the exception information can be obtained in this file.
When the index method throws argrmentexception, the/argerror. VM file is automatically displayed.
That is to say, the rescue attribute can be defined on the class and method. If it is defined on the class, it is valid for all methods in the class. If it is defined in the method, it will overwrite the similar Exception Handling Mechanism in the class.

With this mechanism, we can define an exception class of our own, and then define a parent controller class to specify the rescue attribute. All other controller classes inherit from this class. In this way, we do not need to consider Exception Handling in our code, and will be automatically taken over by the parent class.

Other related points:
When a method does not require this exception mechanism, it can be defined as follows:

[Skiprescue]
Public   Void Skipaction ()
{
//Throw new argumentexception ("dddd ");
Throw NewException ("Fff");
}

You can specify the skiprescue attribute.

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.