-
What is the general reason an exception occur? When an exception appears, does the program terminate immediately or keep running?
异常发生的一般原因是什么?出现异常时,程序是立即终止还是继续运行?
-
I'm guessing @try is the program message I'm testing. Is that right?
我猜@try是我正在测试的程序消息。是对的吗?
-
After @catch either log an error message, clean up, etc, what does the @finally block do? The book I'm reading says that @finally determines whether or not a statement in an @try throws an exception. But isn't that an unnecessary step because with or without it, we could tell whether there was an exception based on the program terminating abruptly?
@catch之后要么记录错误信息,要么清理等等,@ finally块会做什么?我正在阅读的书中说@finally确定@try中的语句是否会引发异常。但这不是一个不必要的步骤,因为有或没有它,我们可以判断是否存在基于程序突然终止的异常?
-
What's the throw directive? The books says it enables you to throw your own exception. But I find that really confusing. Does that mean I could somehow create an exception and test it out?
什么是throw指令?这本书说它可以让你抛出自己的例外。但我发现这真的令人困惑。这是否意味着我可以以某种方式创建一个例外并测试它?
I would really appreciate it if you could answer at least one of the questions for me. Thank you.
如果你能为我回答至少一个问题,我将非常感激。谢谢。
2 个解决方案
#1
5
Exceptions provide a mechanism for handling errors and other unusual events outside the normal flow of execution. They allow the programmer to write cleaner code by eliminating the need to constantly check for and handle errors. Instead, when an error happens, the code can "throw" an exception object, and execution will stop at that point and resume at the first "catch" block that can handle the exception. I could explain further, but I'm sure you'll get a better explanation of exceptions in general by reading the relevant Wikipedia page.
异常提供了一种在正常执行流程之外处理错误和其他异常事件的机制。它们允许程序员通过消除不断检查和处理错误的需要来编写更清晰的代码。相反,当发生错误时,代码可以“抛出”异常对象,并且执行将在该点停止并在可以处理异常的第一个“catch”块处继续。我可以进一步解释,但我相信通过阅读相关的*页面,你会得到更好的例外解释。
So, with that in mind, the directives you mention allow for exceptions in Objective-C:
因此,考虑到这一点,您提到的指令允许Objective-C中的异常:
-
@try: Code in a try block may throw an exception. Try blocks are usually followed by catch blocks which can catch whatever exceptions might be thrown by the code in the try block, but it's also possible that a given exception needs to be handled at a higher level. In that case, the relevant catch block may be somewhere higher up, like in the method that called the method with the try block, or the one above that. (Those methods would also have try blocks that would have called the current method.)
@try:try块中的代码可能会抛出异常。 try块通常后跟catch块,它可以捕获try块中代码可能引发的任何异常,但也可能需要在更高级别处理给定的异常。在这种情况下,相关的catch块可能位于更高的某个位置,就像使用try块调用方法的方法或上面的方法一样。 (那些方法也会有调用当前方法的try块。)
-
@catch: Catch blocks contain code for handling a given exception and hopefully recovering from the exceptional condition.
@catch:Catch块包含用于处理给定异常的代码,并希望从异常条件中恢复。
-
@finally: The finally block contains code that performs any needed cleanup for the try block. For example, if the try block allocated some resources, the finally block could deallocate those resources before allowing the exception to move up to the next catch handler.
@finally:finally块包含为try块执行任何所需清理的代码。例如,如果try块分配了一些资源,则finally块可以在允许异常移动到下一个catch处理程序之前释放这些资源。
-
@throw: This is a directive that's used to "throw" an exception. Execution of the try block stops at the point where an exception is thrown, and the exception will be handled (or not) by a catch block. (Unhandled exceptions generally cause the program to terminate.)
@throw:这是一个用于“抛出”异常的指令。 try块的执行在抛出异常的位置停止,并且catch块将处理(或不处理)异常。 (未处理的异常通常会导致程序终止。)
Now, with all that said, you should usually avoid exceptions in Objective-C. I know it seems strange to have this great feature built into the language and then not be able to use it, but Objective-C programs are almost always written using the Cocoa or Cocoa Touch frameworks, and those frameworks don't expect to have exceptions thrown across framework calls.
现在,尽管如此,您通常应该避免使用Objective-C中的异常。我知道将这个强大的功能内置到语言中然后无法使用它似乎很奇怪,但Objective-C程序几乎总是使用Cocoa或Cocoa Touch框架编写,并且这些框架不希望有异常抛出框架调用。
What is the general reason an exception occur? When an exception appears, does the program terminate immediately or keep running?
异常发生的一般原因是什么?出现异常时,程序是立即终止还是继续运行?
You throw an exception when something unexpected happened that prevents your code from continuing. For example, trying to access the n+1th element in an array with only n items throws an exception. In Cocoa or Cocoa Touch programs, exceptions are terminal. In other languages and frameworks, though, exceptions may be handled to allow the program to continue running.
当发生意外事件而导致代码无法继续时,您会抛出异常。例如,尝试访问仅包含n个项的数组中的第n + 1个元素会引发异常。在Cocoa或Cocoa Touch程序中,例外是终端。但是,在其他语言和框架中,可以处理异常以允许程序继续运行。
After @catch either log an error message, clean up, etc, what does the @finally block do?
@catch之后要么记录错误信息,要么清理等等,@ finally块会做什么?
A finally block runs whether or not an exception is thrown, so you don't need to have cleanup code in both your try block and your catch block. Also, catch blocks often catch exceptions that were thrown at a lower level, so they may not be able to clean up.
无论是否抛出异常,finally块都会运行,因此您不需要在try块和catch块中都有清理代码。此外,catch块通常会捕获较低级别的异常,因此它们可能无法清理。
What's the throw directive? The books says it enables you to throw your own exception.
什么是throw指令?这本书说它可以让你抛出自己的例外。
@throw;
(without a parameter) in a catch block re-throws the same exception that the catch block was handling, so that it can be dealt with at a higher level.
@扔; (在没有参数的情况下)在catch块中重新抛出catch块正在处理的异常,以便可以在更高级别处理它。
It sounds like you're probably reading Programming in Objective-C by Steven Kochan. If I remember correctly, Kochan tries to teach you Objective-C divorced from Cocoa and Cocoa Touch. That might make sense at some level, but this is one case where it'll probably confuse you more than is necessary, especially if you're trying to learn the language so that you can use one or both of those frameworks. Given Apple's advice against using exceptions in your code, it's probably safe to stop reading and move on to the next chapter.
听起来你可能正在阅读Steven Kochan的Objective-C编程。如果我没记错的话,Kochan试图教你与Cocoa和Cocoa Touch脱离的Objective-C。这在某种程度上可能是有意义的,但这是一个案例,它可能会让你更加困惑,特别是如果你试图学习语言,以便你可以使用这些框架中的一个或两个。鉴于Apple建议不要在代码中使用异常,因此停止阅读并继续下一章可能是安全的。
#2
1
Understand that you have a block of code -- between {}
characters -- that may produce a runtime error message. In many systems that runtime error message is either sent to some log and ignored, or it causes the application to terminate abruptly.
了解您有一段代码 - 在{}字符之间 - 可能会产生运行时错误消息。在许多系统中,运行时错误消息要么被发送到某个日志而被忽略,要么导致应用程序突然终止。
With exceptions, the code that detects the error creates an exception object and "throws" it with a throw
statement or method.
除了异常之外,检测错误的代码会创建一个异常对象,并使用throw语句或方法“抛出”它。
If the exception is not handled, it will "bubble" up to the main
level and cause the app to terminate (usually after a default handler prints a message, etc).
如果未处理异常,它将“冒泡”到主级别并导致应用程序终止(通常在默认处理程序打印消息之后等)。
However, you can make your block of code a try
clause (by placing try
or @try
or whatever ahead of the {
), follow that clause with a catch
clause, and then, when the exception occurs, control will be transferred to the start of the catch
clause.
但是,您可以使您的代码块成为try子句(通过在try之前放置try或@try或其他内容),使用catch子句跟随该子句,然后,当异常发生时,控制权将转移到开始catch子句。
The catch
clause can look at the exception object and decide what to do with it. Implementations vary a little, but usually if the catch
clause does nothing the exception will be ignored and execution will continue after the try/catch
clauses. If, on the other hand, the catch
clause cannot "handle" the exception somehow (and hence the exception should not be ignored), the catch
clause will execute a throw
to rethrow the original exception, or a newly created one.
catch子句可以查看异常对象并决定如何处理它。实现略有不同,但通常如果catch子句不执行任何操作,则异常将被忽略,并且在try / catch子句之后将继续执行。另一方面,如果catch子句无法以某种方式“处理”异常(因此不应忽略异常),则catch子句将执行throw以重新抛出原始异常或新创建的异常。
When an exception is not handled by a given try/catch
range, it is "bubbled up" to the next outer try/catch
, in that method or its caller. Eventually an unhandled exception reaches main
and a default handler gets control.
当给定的try / catch范围没有处理异常时,它会“冒泡”到该方法或其调用者的下一个外部try / catch。最终,未处理的异常到达main,默认处理程序获得控制权。
finally
is a feature of most exception handling models, not a critical function, but very convenient. If a try/catch
range is executed without generating an exception, the finally
clause is executed and then execution continues after the finally
clause. On the other hand, if an exception is raised in the try/catch
range and is not handled, the finally
clause is executed and then the exception is "bubbled up", without executing the following code.
最后是大多数异常处理模型的一个特性,不是关键功能,而是非常方便。如果在不生成异常的情况下执行try / catch范围,则执行finally子句,然后在finally子句之后继续执行。另一方面,如果在try / catch范围内引发异常但未处理,则执行finally子句,然后异常“冒泡”,而不执行以下代码。
finally
may be used, eg, to close an open file or release some allocated temporary resource.
最后可以用于例如关闭打开的文件或释放一些分配的临时资源。
#1
5
Exceptions provide a mechanism for handling errors and other unusual events outside the normal flow of execution. They allow the programmer to write cleaner code by eliminating the need to constantly check for and handle errors. Instead, when an error happens, the code can "throw" an exception object, and execution will stop at that point and resume at the first "catch" block that can handle the exception. I could explain further, but I'm sure you'll get a better explanation of exceptions in general by reading the relevant Wikipedia page.
异常提供了一种在正常执行流程之外处理错误和其他异常事件的机制。它们允许程序员通过消除不断检查和处理错误的需要来编写更清晰的代码。相反,当发生错误时,代码可以“抛出”异常对象,并且执行将在该点停止并在可以处理异常的第一个“catch”块处继续。我可以进一步解释,但我相信通过阅读相关的*页面,你会得到更好的例外解释。
So, with that in mind, the directives you mention allow for exceptions in Objective-C:
因此,考虑到这一点,您提到的指令允许Objective-C中的异常:
-
@try: Code in a try block may throw an exception. Try blocks are usually followed by catch blocks which can catch whatever exceptions might be thrown by the code in the try block, but it's also possible that a given exception needs to be handled at a higher level. In that case, the relevant catch block may be somewhere higher up, like in the method that called the method with the try block, or the one above that. (Those methods would also have try blocks that would have called the current method.)
@try:try块中的代码可能会抛出异常。 try块通常后跟catch块,它可以捕获try块中代码可能引发的任何异常,但也可能需要在更高级别处理给定的异常。在这种情况下,相关的catch块可能位于更高的某个位置,就像使用try块调用方法的方法或上面的方法一样。 (那些方法也会有调用当前方法的try块。)
-
@catch: Catch blocks contain code for handling a given exception and hopefully recovering from the exceptional condition.
@catch:Catch块包含用于处理给定异常的代码,并希望从异常条件中恢复。
-
@finally: The finally block contains code that performs any needed cleanup for the try block. For example, if the try block allocated some resources, the finally block could deallocate those resources before allowing the exception to move up to the next catch handler.
@finally:finally块包含为try块执行任何所需清理的代码。例如,如果try块分配了一些资源,则finally块可以在允许异常移动到下一个catch处理程序之前释放这些资源。
-
@throw: This is a directive that's used to "throw" an exception. Execution of the try block stops at the point where an exception is thrown, and the exception will be handled (or not) by a catch block. (Unhandled exceptions generally cause the program to terminate.)
@throw:这是一个用于“抛出”异常的指令。 try块的执行在抛出异常的位置停止,并且catch块将处理(或不处理)异常。 (未处理的异常通常会导致程序终止。)
Now, with all that said, you should usually avoid exceptions in Objective-C. I know it seems strange to have this great feature built into the language and then not be able to use it, but Objective-C programs are almost always written using the Cocoa or Cocoa Touch frameworks, and those frameworks don't expect to have exceptions thrown across framework calls.
现在,尽管如此,您通常应该避免使用Objective-C中的异常。我知道将这个强大的功能内置到语言中然后无法使用它似乎很奇怪,但Objective-C程序几乎总是使用Cocoa或Cocoa Touch框架编写,并且这些框架不希望有异常抛出框架调用。
What is the general reason an exception occur? When an exception appears, does the program terminate immediately or keep running?
异常发生的一般原因是什么?出现异常时,程序是立即终止还是继续运行?
You throw an exception when something unexpected happened that prevents your code from continuing. For example, trying to access the n+1th element in an array with only n items throws an exception. In Cocoa or Cocoa Touch programs, exceptions are terminal. In other languages and frameworks, though, exceptions may be handled to allow the program to continue running.
当发生意外事件而导致代码无法继续时,您会抛出异常。例如,尝试访问仅包含n个项的数组中的第n + 1个元素会引发异常。在Cocoa或Cocoa Touch程序中,例外是终端。但是,在其他语言和框架中,可以处理异常以允许程序继续运行。
After @catch either log an error message, clean up, etc, what does the @finally block do?
@catch之后要么记录错误信息,要么清理等等,@ finally块会做什么?
A finally block runs whether or not an exception is thrown, so you don't need to have cleanup code in both your try block and your catch block. Also, catch blocks often catch exceptions that were thrown at a lower level, so they may not be able to clean up.
无论是否抛出异常,finally块都会运行,因此您不需要在try块和catch块中都有清理代码。此外,catch块通常会捕获较低级别的异常,因此它们可能无法清理。
What's the throw directive? The books says it enables you to throw your own exception.
什么是throw指令?这本书说它可以让你抛出自己的例外。
@throw;
(without a parameter) in a catch block re-throws the same exception that the catch block was handling, so that it can be dealt with at a higher level.
@扔; (在没有参数的情况下)在catch块中重新抛出catch块正在处理的异常,以便可以在更高级别处理它。
It sounds like you're probably reading Programming in Objective-C by Steven Kochan. If I remember correctly, Kochan tries to teach you Objective-C divorced from Cocoa and Cocoa Touch. That might make sense at some level, but this is one case where it'll probably confuse you more than is necessary, especially if you're trying to learn the language so that you can use one or both of those frameworks. Given Apple's advice against using exceptions in your code, it's probably safe to stop reading and move on to the next chapter.
听起来你可能正在阅读Steven Kochan的Objective-C编程。如果我没记错的话,Kochan试图教你与Cocoa和Cocoa Touch脱离的Objective-C。这在某种程度上可能是有意义的,但这是一个案例,它可能会让你更加困惑,特别是如果你试图学习语言,以便你可以使用这些框架中的一个或两个。鉴于Apple建议不要在代码中使用异常,因此停止阅读并继续下一章可能是安全的。
#2
1
Understand that you have a block of code -- between {}
characters -- that may produce a runtime error message. In many systems that runtime error message is either sent to some log and ignored, or it causes the application to terminate abruptly.
了解您有一段代码 - 在{}字符之间 - 可能会产生运行时错误消息。在许多系统中,运行时错误消息要么被发送到某个日志而被忽略,要么导致应用程序突然终止。
With exceptions, the code that detects the error creates an exception object and "throws" it with a throw
statement or method.
除了异常之外,检测错误的代码会创建一个异常对象,并使用throw语句或方法“抛出”它。
If the exception is not handled, it will "bubble" up to the main
level and cause the app to terminate (usually after a default handler prints a message, etc).
如果未处理异常,它将“冒泡”到主级别并导致应用程序终止(通常在默认处理程序打印消息之后等)。
However, you can make your block of code a try
clause (by placing try
or @try
or whatever ahead of the {
), follow that clause with a catch
clause, and then, when the exception occurs, control will be transferred to the start of the catch
clause.
但是,您可以使您的代码块成为try子句(通过在try之前放置try或@try或其他内容),使用catch子句跟随该子句,然后,当异常发生时,控制权将转移到开始catch子句。
The catch
clause can look at the exception object and decide what to do with it. Implementations vary a little, but usually if the catch
clause does nothing the exception will be ignored and execution will continue after the try/catch
clauses. If, on the other hand, the catch
clause cannot "handle" the exception somehow (and hence the exception should not be ignored), the catch
clause will execute a throw
to rethrow the original exception, or a newly created one.
catch子句可以查看异常对象并决定如何处理它。实现略有不同,但通常如果catch子句不执行任何操作,则异常将被忽略,并且在try / catch子句之后将继续执行。另一方面,如果catch子句无法以某种方式“处理”异常(因此不应忽略异常),则catch子句将执行throw以重新抛出原始异常或新创建的异常。
When an exception is not handled by a given try/catch
range, it is "bubbled up" to the next outer try/catch
, in that method or its caller. Eventually an unhandled exception reaches main
and a default handler gets control.
当给定的try / catch范围没有处理异常时,它会“冒泡”到该方法或其调用者的下一个外部try / catch。最终,未处理的异常到达main,默认处理程序获得控制权。
finally
is a feature of most exception handling models, not a critical function, but very convenient. If a try/catch
range is executed without generating an exception, the finally
clause is executed and then execution continues after the finally
clause. On the other hand, if an exception is raised in the try/catch
range and is not handled, the finally
clause is executed and then the exception is "bubbled up", without executing the following code.
最后是大多数异常处理模型的一个特性,不是关键功能,而是非常方便。如果在不生成异常的情况下执行try / catch范围,则执行finally子句,然后在finally子句之后继续执行。另一方面,如果在try / catch范围内引发异常但未处理,则执行finally子句,然后异常“冒泡”,而不执行以下代码。
finally
may be used, eg, to close an open file or release some allocated temporary resource.
最后可以用于例如关闭打开的文件或释放一些分配的临时资源。