Always given:Exception Handling

This lesson we get to know about the Swift error handling

In calling the method and the time to write a wheel,There will always be wrong sorts of weird,Normal is already compiled software,Some unexpected errors also occur。but,Among these errors,There are some that can be identified and captured - they can be expected。

Predictable error

Why do we say that there are some errors that can be expected to get it? For example, reading a file when the file does not exist、Save a document when the directory is not writable、Download the file when no network connection、When transferring a parameter range beyond the control of these errors are to be expected ...... you get! We did not need to make these mistakes in the program to crash in!

Then,In these cases we need a complete exception handling mechanism,These anomalies can be expected to catch up gave,In a friendly way to dispose of,Avoid planting in our program on these known errors。

Throw an error and catch errors

We call a method,if needed,You have to understand that this method is risky,It might have failed - the reason this time it will fail to throw - throw an error。

You have to throw it to the wrong situation may well have to consider,If this error,How to do,If that error,And how to do!

So,If we create a method may be risky,It must use throws flagged,This time we can use to call the catch to catch the bomb。

Creates an enum that contains the error status

We use enumerated types to create a possible error conditions,Just let enumeration protocol to follow ErrorType,Others are to complete compiler。

For example, we write here two errors,A barrel is overheating,Another is no bullets!

Risky method

Then we come to the transformation of fire Gun() Technique:

A simple model,Check the amount of ammunition before each launch,Check the temperature of the barrel,If you are OK,On fire,At the same time the barrel temperature increase,Reduce the amount of ammunition。(This temperature setting is not necessarily an anomaly ha)

Processing error

Then we call this method 11 times to see what kind of happens:

Final,Our results are to:

Guard if it is?


As you can see,Guard syntax works if it seems like a,But it is not,You can understand if it was opposite , If we write this in turn it is entirely possible:

Is not it seem around? Piles of braces feel dizzy watching it!

Use the Guard is like a goalkeeper - we call him "gatekeepers mode",In fact, it seemed as if in turn use if:

Although such as the,Did you notice? Greater than less than the number we gave flipped! Because it can make if we want to play a role,or,We can use?

The logic of this size is straightened,But the extra exclamation mark leaving a big head,But this is also easy to accidentally disturb Waner ......

So,We use the Guard,It is also a non-determination,If "yes" is not the role of,If the "non",Else on the implementation of the contents,Logic in this line of people very intuitive - no need Quxianjiuguo。

To put at risk do – catch block

Like my previous example,We can transform fire method to method throws an exception,So call it when you have to put it in a special container which,So that the compiler can understand and get those errors。

Above,We do block of code which attempts method (the try),If normal that everyone is happy。If the method throws an exception,Then we will execute on the catch - here actually looks like if the nesting else,It seems to switch choice - but,catch does require the might throw errors written down if you do not,Then the bomb landed your program also will be blown it!

but,Like just switch,You can also write not only catch with the wrong type,This will default to match all wrong - the downside is that you can not know what went wrong。

Communication with the caller

That we do this complex actions what is the point? Written directly to a state which is not better?

Not really better。Because our approach is not necessarily for their own use,A program will not be just one person to complete,When someone calls your method when,You do have to let people read the first 800 words of the document? In the desired position throws an error,Write clearly wrong type - the caller can easily dispose of this issue。


We say that we should try encounter risk,And try out the error to catch! But the bomb did not catch live how to do? If you do not want to bomb it? In fact be,If you call a risky method in your approach in,And you do not want to In this method Bomb,Then leave it alone just fine - of course,Remember to put your mark throws methods ,So the bomb will continue to pass down!

Uh,But you have to remember,Eventually you have to have a way to catch and bomb,otherwise,Your program will still be blown up。

If a problem can not be resolved,It will not go away。

correct,If you do not want to throw an error,You can try to force,Use the keyword try! ,This eliminates the need ...... because the bomb without throwing bombs encountered,Direct blow。

Original article written by Gerber drop-off:R0uter's Blog » Always given:Exception Handling

Reproduced Please keep the source and description link:

About the Author


The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *