我知道有一个开始救援结束的标准技术
如何单独使用救援区。
它是如何工作的以及它如何知道正在监视哪些代码?
答案 0 :(得分:212)
方法“def”可以作为“开始”语句:
def foo
...
rescue
...
end
答案 1 :(得分:47)
你也可以内联救援:
1 + "str" rescue "EXCEPTION!"
将打印出“EXCEPTION!”因为'String不能强制进入Fixnum'
答案 2 :(得分:25)
我正在使用def / rescue组合与ActiveRecord验证:
def create
@person = Person.new(params[:person])
@person.save!
redirect_to @person
rescue ActiveRecord::RecordInvalid
render :action => :new
end
我认为这是非常精益的代码!
答案 3 :(得分:17)
示例:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end
此处,def
为begin
声明:
def
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end
答案 4 :(得分:1)
奖金!您也可以使用其他种类的块来执行此操作。例如:
#rightModal .modal-dialog-slideout {
min-height: 100%;
margin: 0 0 0 auto;
background: #fff;
}
.modal.fade .modal-dialog.modal-dialog-slideout {
-webkit-transform: translate(100%, 0)scale(1);
transform: translate(100%, 0)scale(1);
}
.modal.fade.show .modal-dialog.modal-dialog-slideout {
-webkit-transform: translate(0, 0);
transform: translate(0, 0);
display: flex;
align-items: stretch;
height: 100%;
}
.modal.fade.show .modal-dialog.modal-dialog-slideout .modal-body {
overflow-y: auto;
overflow-x: hidden;
}
.modal-dialog-slideout .modal-content {
border: 0;
}
.modal-dialog-slideout .modal-header,
.modal-dialog-slideout .modal-footer {
height: 69px;
display: block;
}
#rightModal .modal-dialog-slideout .modal-header h5 {
float: left;
color: blue;
}
#leftModal .modal-dialog-slideout {
min-height: 100%;
margin: 0 auto 0 0;
background: #fff;
}
#leftModal .modal-dialog-slideout .modal-header h5 {
float: left;
color: red;
}
#leftModal.modal.fade:not(.show) .modal-dialog.modal-dialog-slideout {
-webkit-transform: translate(100%, 0)scale(1);
transform: translate(-100%, 0)scale(1);
}
在<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<h1>Some heading</h1>
<p>This is not a love song</p>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#leftModal">Left</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#rightModal">Right</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#middleModal">Middle</button>
</div>
</div>
</div>
<!-- Right Modal -->
<div class="modal fade" id="rightModal" tabindex="-1" role="dialog" aria-labelledby="rightModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-slideout" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="rightModalLabel">Right On</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Right Modal
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- Left modal -->
<div class="modal fade" id="leftModal" tabindex="-1" role="dialog" aria-labelledby="leftModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-slideout" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="leftModalLabel">Left On</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Left Modal
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- Button trigger modal -->
<!-- Middle Modal -->
<div class="modal fade" id="middleModal" tabindex="-1" role="dialog" aria-labelledby="middleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="middleModalLabel">Middle Modal</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
中输出:
[1, 2, 3].each do |i|
if i == 2
raise
else
puts i
end
rescue
puts 'got an exception'
end