I just started right now with the topic exceptionhandling. I read alot of positiv stuff about it, so I thought I should do the same, because my current technique using errorcodes is really ugly...
Okay we have following scenario: A user typed his password in a textbox. After pressing the login button, he gets an positiv or negativ message.
MainWindow.xaml
<TextBox x:Name="txtBoxUserPassword/>
<Button x:Name="btnLogin" Click="btnLogin_Click">Login</Button>
MainWindow.xaml.cs
private void btnCT_Click(object sender, RoutedEventArgs e)
{
DataBase db = new DataBase();
db.IsPasswordCorrect(this.txtBoxUserPassword.Text);
// How can I catch here the exception from the class to
// show the error notification?
}
DataBase.cs
public class DataBase{
public void IsPasswordCorrect(string password)
{
try{
if(password != "fooBar")
{
throw new InvalidPasswordException("You entered the wrong password. Try it again.");
}
else
{
/*
...
*/
}
}
catch(InvalidPasswordException ex){
// What should I do here? I want to give the user an
// error message with the content "You entered the wrong
// password. Try it again."
}
}
}
InvalidPasswordException.cs
public class InvalidPasswordException: Exception
{
public InvalidPasswordException(string message, Exception inner)
: base(message, inner)
{
}
}
As you can see, it's the first time I using exceptions. Hopefully you can help me a bit. Thanks!
Edit
I have this switch/case construct inside my public void btnCT_Click().
switch (CheckString("EnteredString"))
{
case 1:
MessageBox.Show("Error 1");
break;
case 2:
MessageBox.Show("Error 2");
break;
case 3:
MessageBox.Show("Error 3");
break;
case 0:
MessageBox.Show("Successful");
break;
}
Here is my method from another class. The classname is not important.
public int CheckString(string enteredString)
{
if(enteredString length is larger 25 chars)
return 1;
else if(enteredString contains special characters)
return 2;
else if(enteredString dosent contain any number)
return 3;
else
return 0;
}
The first thing that I have to say is that you don't need a custom exception just for this. Looking just to the method's name (IsPasswordCorrect) anyone would expect this method to return a true/false boolean value not an exception.
So you could have a simpler
public class DataBase
{
public bool IsPasswordCorrect(string password)
{
if(password != "fooBar")
return false;
else
return true;
}
}
.... at the UI level ....
if(!db.IsPasswordCorrect("notA_fooBar"))
MessageBox.Show("You entered the wrong password. Try again");
else
....
However, if you really need to thrown an exception (remember that this is a costly operation in terms of performances) then do not catch it in the same method where you throw it but let it bubble up to reach the calling code
public bool IsPasswordCorrect(string password)
{
if(password != "fooBar")
throw new InvalidPasswordException("You entered the wrong password. Try again");
....
}
and in the calling code (at the UI level) add the try catch blocks
private void btnCT_Click(object sender, RoutedEventArgs e)
{
try
{
DataBase db = new DataBase();
db.IsPasswordCorrect(this.txtBoxUserPassword.Text);
// If you reach this point the password is correct
// But it is ugly and unclear to any reader....
}
catch(InvalidPasswordException ex)
{
MessageBox.Show(ex.Message);
}
In any case exceptions should be used only for exceptional reasons. You don't use them to drive your code. In this context the best method is to return a true/false value.
When the failure reasons are complex I usually use this approach
bool ok = true;
ok = database.IsPasswordCorrect("fooBar");
if(ok) ok = database.Method1();
if(ok) ok = database.Method2();
if(ok) ok = database.Method3();
if(!ok)
MessageBox.Show(database.LastErrorMessage);
public class Database
{
public string LastErrorMessage { get; set; }
public bool Method1()
{
if(errorFoundForReason1)
{
LastErrorMessage = "Error found for reason1";
return false;
}
if(errorFoundForReason2)
{
LastErrorMessage = "Error found for reason2";
return false;
}
....
return true;
}
}
Of course, every boolean method in the database class, when there is a reason to fail and before returning false, sets a global variable with the exact error message involved in the failure so the client code could handle easily the message when it has finished the calls.
Related
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
I have an SSIS package which calls a Data Flow Task as part of a loop which iterates different end-point addresses (out of scope).
The Data Flow Task has a source Script Component responsible for calling a REST API and creating a row for each result.
There are 3 output buffers;
1. actual data row
2. error row
3. monitoring
The monitoring buffer used for telemetry and is populated through an event (EventHander) that is fired every time the API makes a request.
During the first iteration of the ForEach int the Control Flow loop, everything runs as expected, all the buffers produce the correct rows.
However, during the next iterations, the monitoring buffer which is populated within the event throws;
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.HandleUserException(Exception e)
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PrimeOutput(Int32 outputs, Int32[] outputIDs, PipelineBuffer[] buffers)
at Microsoft.SqlServer.Dts.Pipeline.ManagedComponentHost.HostPrimeOutput(IDTSManagedComponentWrapper100 wrapper, Int32 outputs, Int32[] outputIDs, IDTSBuffer100[] buffers, IntPtr ppBufferWirePacket)
I don't understand why the MonitoringBuffer is not initialised in the proceeding iterations.
The exception occurs while calling MonitoringBuffer.AddRow();.
Here's the whole Script Component simplified for readability:
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
private string ClientCode { get { return Variables.ErplyClientCode; } }
private string Username { get { return Variables.ErplyUsername; } }
private string Password { get { return Variables.ErplyPassword; } }
private bool IsTest { get { return Variables.IsTest; } }
private int ErplyRecordsPerPage { get { return Variables.ErplyRecordsPerPage; } }
private string ErplyDebugOutputPath { get { return Variables.ErplyDebugOutputPath; } }
private DateTime ChangeSince { get { return Variables.ChangeSince; } }
private int records { get; set; }
private int errors { get; set; }
private string rawFolder { get; set; }
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
}
public override void CreateNewOutputRows()
{
ErplyAPI.OnPreRequestEvent += new EventHandler<EAPIEvent>(ErplyAPI_OnPreRequestEvent);
var staff = ErplyAPI.getStaff(ClientCode, Username, Password, ChangeSince, ErplyRecordsPerPage, IsTest);
foreach (var p in staff.List)
{
try
{
if (!p.IsError)
{
EmployeeBuffer.AddRow();
EmployeeBuffer.employeeID = p.employeeID;
}
else
{
ErrorBuffer.AddRow();
ErrorBuffer.employeeID = p.employeeID;
ErrorBuffer.Error = p.Error.Message.Trim() + "\n" + p.Error.StackTrace;
errors++;
}
records++;
}
catch (Exception ex)
{
this.ComponentMetaData.FireWarning(0, "Script", ex.Message + "\n" + ex.StackTrace, string.Empty, 0);
}
}
EmployeeBuffer.SetEndOfRowset();
ErrorBuffer.SetEndOfRowset();
}
private void ErplyAPI_OnPreRequestEvent(object sender, EAPIEvent e)
{
var request = string.Empty;
var sessionKey = string.Empty;
bool fireAgain = true;
if (e == null)
{
ComponentMetaData.FireWarning(0, "SC_ERPLY_API", string.Format("EAPIEvent is NULL in ErplyAPI_OnPreRequestEvent. Amonit did not log the Erply request."), string.Empty, 0);
return;
}
if (e.eAPI == null)
{
ComponentMetaData.FireWarning(0, "SC_ERPLY_API", string.Format("EAPIEvent.eAPI is NULL in ErplyAPI_OnPreRequestEvent. Amonit did not log the Erply request."), string.Empty, 0);
return;
}
try
{
if (e.Parameters != null && e.Parameters.ContainsKey("request"))
request = e.Parameters["request"].ToString();
if (request != "verifyUser" && e.Parameters != null && e.Parameters.ContainsKey("sessionKey"))
sessionKey = e.Parameters["sessionKey"].ToString();
}
catch (Exception ex)
{
ComponentMetaData.FireWarning(0, "SC_ERPLY_API", string.Format("Error occurred assigning variables from EAPIEvent parameters in ErplyAPI_OnPreRequestEvent. {0} {1}", ex.Message, ex.StackTrace), string.Empty, 0);
}
try
{
MonitoringBuffer.AddRow(); // Exception occurs here
MonitoringBuffer.Request = ResizeString(request, 255);
MonitoringBuffer.SessionKey = ResizeString(sessionKey, 128);
}
catch (Exception ex)
{
var message = string.Format("Error occurred outputting Erply request in ErplyAPI_OnPreRequestEvent. {0} {1}", ex.Message, ex.StackTrace);
MonitoringBuffer.ErrorMessage = ResizeString(message, 8000);
ComponentMetaData.FireWarning(0, "SC_ERPLY_API", message, string.Empty, 0);
}
finally
{
MonitoringBuffer.EndOfRowset();
}
}
}
I sorted the problem out.
The exception was being raised when the variable dispenser was being accessed from the Event. For some reason the GetValueWithContext(ScriptComponent.EvaluatorContext) is being dropped during the second call. Why this happens is beyond me.
The solution is simple, assign the variables from the variables dispenser to a local property or variable in the OnPreExecute function.
It's also good practice to not call the variable dispenser in the CreateNewOutputRows as it cause variable locking.
I ran into this issue too, but my solution was a little different -- moving the variable assignments into PreExecute() didn't help.
Instead, what I'd done is that I wanted to parse three different files, and read each of them with a Script Component. Their columns were kinda similar, so I created one Data Flow task, made sure it worked, then copied it and modified each copy to reflect the differences in the files. Running each individual Data Flow task was successful, but when I tried to run two of them, one after the other in a loop, I got a NullReferenceException from HostPrimeOutput() after calling the OutputBuffer.AddRow() method in my Script Component.
It turns out that when I copied each Data Flow task, the Script Components all kept the same namespace, and I guess it doesn't like that. So, I created brand new Script Components, set up all the output columns again (ugh!), copied the body of the script over, and it's happy.
I have an exception occurred when the Database connection failed in a Class. The problem is how do I notify my Main Window that this exception is caught and show a message box to notify my user?
Thanks
Use the Try ... Catch clause like this:
try
{
// The code that could generate an exception
}
catch(Exception ex)
{
MessageBox.Show("Error: " ex.Message);
}
Or if you're using SQL-Server connection, use it like this:
try
{
// The code that could generate an exception
}
catch(SqlException ex)
{
MessageBox.Show("SQL Error: " ex.Message);
}
Thanks. I may have not make my question clearly. I mean this exception
is occurred in one class, but the message box should be show in an
other windows class. So how do I communicate and show this error?
From your clarification in one of the comments:
So if you have class TestClass.cs with method Test in it.
public void Test()
{
//if you want to throw an exception defined by your business logic
if(someCondition == false)
throw CustomException();
//if you have exception in the code
int a = 5;
int b =0;
//here you will be thrown an exception can't divide by 0.
int c = a/b;
}
Your winform Button Click or whatever
public void Button_Click1(object sender, EventArgs e)
{
try
{
TestClass cl = new TestClass();
cl.Test();
}
catch(CustomException custEx)
{
//this for your Bussines logic exception
//write your message
}
catch(DivideByZeroException div)
{
//this for divide by zero exception
//write message
}
//you can catch all other exception like this but I don't advice you to do that
catch(Exception ex)
{
//for this to working properly, this catch should be under all of others(last priority)
}
}
I am doing a project on C#. It's a project to send message using GSM Modem. In my project, I have several function that may give errors. I have used a string variable to store error message. The basic flow is like this.
class SMS{
string message;
public string getport(){
if(ErrorCondition)
message="Error Finding port";
return port;
}
public void executecommand(command){
if(ErrorCondition1)
message="Error Executing Command";
else(ErrorCondition2)
message="No Response from device";
}
public void sendMessage(int number, string text, out string error){
findport();
executecommand(command);
error = message;
}
}
NB:This is not the working code
This is the way I have thought to collect error message, but I not sure if I am doing it the right way. I need your help on this. Is there a better approach? Error string does not save any string.
Update: As the answers of Exception are coming, I am confused about arrangement of try catch, here I have my actual code so that I'll help to give further suggestions.
public string ReadResponse(SerialPort port, int timeout)
{
string buffer = string.Empty;
try
{
do
{
if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length > 0)
message = "Response incomplete";
else
message = "Error on Modem";
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
Your (pseudo) code can be refactored to use the de facto way of managing errors in C#, which are exceptions. Based on your code right there, you're used to using a special variable or return value as an error message and there's nothing wrong with that, it just won't be what other C# developers would expect.
Here's some (pseudo) code that demonstrates what I'm typing about.
class SMS {
public string GetPort() {
if (ErrorCondition) {
throw new PortNotFoundException("Error finding port.");
}
return port;
}
public void ExecuteCommand(Command command) {
if(ErrorCondition1) {
throw new UnknownErrorException("Error Executing Command");
}
else(ErrorCondition2) {
throw new NoResponseException("No Response from device");
}
}
public void SendMessage(int number, string text) {
FindPort();
ExecuteCommand(command);
}
}
You'll note that there's no out variable in SendMessage to handle passing out the error to calling code. That's handled by the thrown exceptions. The calling code is at liberty to determine how best to handle the exception.
You can declare
string message;
to
private List<string> message;
and add message to list, will help to catch multiple errors as well. Also make it private so that other class cant access the same.
the question was :
The application should ask the user for the total number of tickets to be booked. while the booking the tickets if the total number of booked tickets exceeds the available tickets, the application should raise an exception. I don't know why it is not showing an error when I do this I came up with this solution:
using System;
namespace Ticket
{
class blah
{
public void abc()
{
int numberOfTickets;
int numberOfAvailableTickets=10;
int cost = 100;
int pay;
Console.WriteLine("how many tickets do you need");
numberOfTickets = Convert.ToInt32(Console.ReadLine());
try
{
if (numberOfTickets < numberOfAvailableTickets)
{
pay = 100 * numberOfTickets;
Console.WriteLine("Pay please");
Console.WriteLine(pay);
}
}
if( numberOfTickets>numberOfAvailableTickets)
{
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
class Theater
{
static void Main(string[] args)
{
blah hi = new blah();
hi.abc();
Console.ReadLine();
}
}
}
I am not even sure that the code you show even compiles... try this
using System;
namespace Ticket
{
class blah
{
public void abc()
{
int numberOfTickets;
int numberOfAvailableTickets=10;
int cost = 100;
int pay;
Console.WriteLine("how many tickets do you need");
numberOfTickets = Convert.ToInt32(Console.ReadLine());
try
{
if( numberOfTickets>numberOfAvailableTickets)
throw new Exception ("Not enough Tickets available!");
pay = 100 * numberOfTickets;
Console.WriteLine("Pay please");
Console.WriteLine(pay);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
class Theater
{
static void Main(string[] args)
{
blah hi = new blah();
hi.abc();
Console.ReadLine();
}
}
}
It throws an Exception if the the entered number exceeds the available tickets...
You have to use throw to raise an exception :
if( numberOfTickets>numberOfAvailableTickets)
throw new Exception();
The question is telling you to throw an exception if the booked number exceeds the available number.
So you don't need any try or catch in abc (I really hope these names aren't in your real code). You can also remove the if (numberOfTickets < numberOfAvailableTickets) (but keep the code inside.
Above:
pay = 100 * numberOfTickets;
move up:
if( numberOfTickets>numberOfAvailableTickets)
{
Inside the if put:
throw new ArgumentException("numberOfTickets is greater than numberOfAvailableTickets");
You can change ArgumentException to a custom exception if desired
Also note if you're using a catch, it must always be immediately after a try or another catch. You can't have an if between. See the documentation.
In Main, you can either catch that exception, or let it terminate the app (it's not clear from what you provided).
The problem is that you didn't explicitly throw the exception. Unless you do that, the compiler sees nothing wrong with your code, as by default it would only throw exceptions which actually affect the running state of your program.
Although this is a 'quick fix' so to say, just adding a throw new Exception(); where you want the exception to be thrown will work.
However, ideally, I would recommend creating a custom Exception class for this purpose. But the previous line should work anyway :)
I would suggest you to remove the try & catch and simple use MessageBox.
if (numberOfAvaiableTickets < numberOfTickets)
{
MessageBox.Show("Number of tickets exceeded", "ErrorWindow");
}
else
{
pay = 100 * numberOfTickets;
Console.WriteLine("Pay please");
Console.WriteLine(pay);
}
In Delphi I could do something like this:
try
if not DoSomething then
Exit;
if not DoSomething2 then
Exit;
if not DoSomething3 then
Exit;
finally
DoSomethingElse;
end;
In other means if method DoSomething results false then the program flow is transffered to the finally block and DoSomething2 and DoSomething3 are not executed.
How to achieve such behaviour in C#?
Thanks in advance.
Edit1:
The below example doesn't compile in VS 2008
Edit2: I am sorry I was to fast and forget the return statement;
XElement OrderStatus(q_order_status Request)
{
XElement Response;
try
{
if (DoSomething() != 0 )
{
return;
}
}
catch(Exception e)
{
// catch some errors and eventually pass the e.Message to the Response
}
finally
{
Response = new XElement("SomeTag", "SomeResponse");
}
return Response;
}
Edit3:
After testing it seems that the easiest way to achieve this is to throw an exception if the result of DoSomething1 is false. I can throw my own execption, write a specific message and pass it to the finally clause.
You really shouldn't be using exception handling constructs for flow control. That said, Exit is comparable to return in C#. As the MSDN Documentation about the [return keyword][1] says:
If the return statement is inside a try block, the finally block, if one exists, will be executed before control returns to the calling method.
In general a finally-block will almost always execute if the corresponding try-block has been reached. There are a few rare situations where it is impossible to guarantee that the finally-block executes, but they are all fatal errors, upon which programs should likely immediately crash.
How your code would look in C#:
try
{
if (!DoSomething())
return;
if (!DoSomething2())
return;
if (!DoSomething3())
return;
}
finally
{
DoSomethingElse();
}
But again, don't do this. try and finally are intended for handling exceptions, not for normal flow control.
Reply to your edit:
In your code return doesn't compile because the return type of the method is XElement and return by itself can only be used when the return type is void. You could use return new XElement("SomeTag", "SomeResponse");, as that is what the finally would be doing anyway, or you could assign Response earlier and do return Response;.
Note though that while the finally always executes, the return Response; that comes after it doesn't execute if the reason went into the finally-block is because you did a return inside the try-block.
Answer to updated question:
The reason you're having trouble doing this in an elegant way, is because you seem to be using a combination of return values and exceptions. You should consider manually raising an exception instead of using return values if the sitation is, well, exceptional.
Assuming there is a good reason for the return values however, I'm thinking it might be clearer to go without a finally block altogether, and to include a return at the end of the try block and also in your catch block. That would save you from passing the exception message in a messy way.
I can't really say what the best solution would be, since your code snippet does not show what Response would be if DoSomething() returns a non-zero value.
Original answer:
It depends a little on what you're trying to accomplish. Are exceptions actually being thrown in any of the methods? Otherwise there is no good reason to use a try-finally pattern. This would be equivalent (though maybe not advisable for readability):
bool doneEverything = DoSomething() && DoSomething2() && DoSomething3();
DoSomethingElse();
If there are exceptions being thrown, and handled at a higher level, I'd recommend isolating this code in a separate method, so you can use a return statement*.
void DoStuff()
{
try
{
if (!DoSomething())
return;
if (!DoSomething2())
return;
if (!DoSomething3())
return;
}
finally
{
DoSomethingElse();
}
}
To answer your question about when the finally code block is executed: it is always executed, unless the executing thread terminates prematurely.
*: Some restructuring is recommended, because there is no equivalent of the Delphi Exit. The break statement comes closest, but it can only be used in loop constructs or switch blocks. To mimic Exit behavior, you would need goto and a label. We wouldn't want that, now would we? :)
Why not make the three try-lines a common if/else block? Instead of exit, call the DoSomethingElse. Like so:
if (DoSomething() == false)
{
DoSomethingElse();
}
else if (DoSomething2() == false)
{
DoSomethingElse();
}
else if (DoSomething3() == false)
{
DoSomethingElse();
}
I would like to say that "C# is not Delphi", but that would be a bit arrogant.
In C#, finally is executed as well when return is called inside the try statement.
bool doSomething = false;
bool doSomething2 = true;
try
{
if( !doSomething )
{
Console.WriteLine ("not dosomething");
return;
}
if( !doSomething2 )
{
Console.WriteLine ("not dosomething 2");
return;
}
}
finally
{
Console.WriteLine ("In finally");
}
What about switch case of course If you don't mean the finally in c# by saying finally block. default case is the finally block then and you can also find flow control example and here at msdn : Flow Control (C# vs. Java)
static void Main(string[] args)
{
switch (args[0])
{
case "copy":
//...
break;
case "move":
//...
goto case "delete";
case "del":
case "remove":
case "delete":
//...
break;
default:
//...
break;
}
}
In this sort of situation, understanding the question as dealing exclusively with the non-exception handling case, I would refactor the contents of the try into a private helper method, like this
void BranchOnContext()
{
if (!DoSomething())
return;
if (!DoSomething2())
return;
// last one will drop out and return anyway
DoSomething3();
}
void DoStuff()
{
BranchOnContext(); // Assumed not to throw
DoSomethingElse(); // Always the next thing to be executed
}
EDIT -- tracking the changed requirement
void DoStuff()
{
string message = string.Empty;
try {
BranchOnContext();
} catch (MyExpectedException me) { // only catch exceptions I'm prepared to handle
message = me.Message;
}
DoSomethingElse(message); // Always the next thing to be executed
}
Taking another crack at this with the updated info:
I want DoSomethingElse to be executed
always and I want it to include
message from possible exception
If any of the DoSomething's return 0, null is returned. If not, the generic message is created. If there was an exception, it is caught and a message with its info is returned. How about this?
XElement OrderStatus(q_order_status Request)
{
try
{
if (DoSomething() != 0 )
{
return null;
}
else
{
return new XElement("SomeTag", "SomeResponse");
}
}
catch(Exception e)
{
// catch some errors and eventually pass the e.Message to the Response
return new XElement(e.tag, e.response);
}
}
Im still struggling with how to, in a good way, put finally into this.
I find it quite similar in behavior to the Delphi's one which I have shown on the beginning. I am interested in your comments. Response is dependent on the DoSomethings result.
XElement OrderStatus(q_order_status Request)
{
XElement Response;
int result = 0;
string Message = "";
try
{
result = DoSomething1();
if (result != 0)
{
throw new DoSomethingException("DoSomething1 has failed!");
}
result = DoSomething2();
if (result != 0)
{
throw new DoSomethingException("DoSomething2 has failed!");
}
result = DoSomething3();
if (result != 0)
{
throw new DoSomethingException("DoSomething3 has failed!");
}
Message = "All tests has been passed.";
}
catch(DoSomethingException e)
{
Message = e.Message;
}
catch(Exception e)
{
Message = e.Message;
}
finally
{
Response = new XElement("SomeTag", Message);
}
return Response;
}
What do you think?
void funcA()
{
if (!DoSomething())
return;
if (!DoSomething2())
return;
if (!DoSomething3())
return;
}
void funcB()
{
funcA();
DoSomethingElse;
}
This appears to replicate the delphi:-
try
{
if(DoSomething())
if(DoSomething2())
DoSomething3();
}
finally
{
DoSomethingElse();
}
an alternate style (some people will hate this style, others will love it.):-
try
{
DoSomething() && DoSomething2() && DoSomething3();
}
finally
{
DoSomethingElse();
}
I get the impression you want some other behaviour though?
Goto version?
try
{
if (!DoSomething())
goto Exit;
if (!DoSomething2())
goto Exit;
if (!DoSomething3())
goto Exit;
Exit:;
}
finally
{
DoSomethingElse();
}
Note the irritating ; after the label, it seems a label must precede a statement.
Just had an epiphany:-
Func<bool>[] somethings = new Func<bool>[] {DoSomething, DoSomething2, DoSomething3};
try
{
foreach (Func<bool> something in somethings)
{
if (!something())
break;
}
}
finally
{
DoSomethingElse();
}