in a try-block, how to do sth. if no exception occurs ? obj-c
in a standard try-catch-error-block, how can i advice the programm to do开发者_如何学C something only, if no error is thrown ?
for example, if i want to configure a proxy for something ip-based, and if it all works, it should grey-out the button.
set a variable to true first, i.e. noError = true
if any errors occurred, set it to false
I'd do it this way (c#, but the idea can be reused somewhere else)
try {
try {
// some code
}
catch
{ throw; }
// code, done only if there was no error
}
catch {
// read the exception.
}
You simply put it into the try
block, but after the statement that might throw. If it does, control flow will divert to the catch block and skip the later instruction. That't the way try
is supposed to be used.
Of course, if you have multiple statements that might throw exceptions, and just stuff everything in one big hairy global try block, it becomes more difficult to identify the right spot. This is one of the reasons why the huge global try block is an Antipattern.
ok
@Yossarian doesn't work.
@PeterWong yours works. the compiler is just not able to interpret if(noerror), it had to be if(noerror==false)
精彩评论