Еще один способ достижения той же цели: гибкие боксы . Сделайте контейнер гибким блоком столбца, и тогда вы будете иметь полную свободу, позволяя некоторым элементам иметь фиксированный размер (поведение по умолчанию) или заполнять / уменьшать пространство контейнера (с помощью flex-grow: 1 и flex- термоусадочные:. 1)
#wrap {
display:flex;
flex-direction:column;
}
.extendOrShrink {
flex-shrink:1;
flex-grow:1;
overflow:auto;
}
См. https://jsfiddle.net/2Lmodwxk/ (попробуйте расширить или уменьшить окно, чтобы заметить эффект)
Примечание: вы также можете использовать сокращенную свойство:
flex:1 1 auto;
Here's a test class that shows that (1) finally runs, regardless of whether exceptions are thrown; and (2) exceptions are passed along to the caller.
public class FinallyTest extends TestCase {
private boolean finallyWasRun = false;
public void testFinallyRunsInNormalCase() throws Exception {
assertFalse(finallyWasRun);
f(false);
assertTrue(finallyWasRun);
}
public void testFinallyRunsAndForwardsException() throws Exception {
assertFalse(finallyWasRun);
try {
f(true);
fail("expected an exception");
} catch (Exception e) {
assertTrue(finallyWasRun);
}
}
private void f(boolean withException) throws Exception {
try {
if (withException)
throw new Exception("");
} finally {
finallyWasRun = true;
}
}
}
The finally
code will always run, and exceptions will be passed on up, as you say. That's pretty much the point of try/finally
- to have some code that will always run, even when exceptions are thrown.
Edit: This is true for any language that provides the try/finally
construct, but there are caveats for some languages, as Adam points out in his comment and Sam points out in his answer.
Если это C # :
Здесь правильные ответы, выполняется команда finally и исключения «пропускаются». Но чтобы проиллюстрировать, насколько легко это понять:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception("testing");
}
finally
{
Console.WriteLine("Finally");
}
}
}
При запуске этого простого небольшого консольного приложения генерируется исключение, а затем выполняется блок finally.
Предполагая, что это C #, в конечном итоге всегда будет выполняться, если вы не получите StackOverflowException или ExecutingEngineException
Кроме того, асинхронные исключения, такие как ThreadAbortException, могут прервать поток блок finally, вызывающий его частичное выполнение.
См. Связанные вопросы: