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.