About Exception
来源:互联网 发布:scala编程思想中文版 编辑:程序博客网 时间:2024/06/14 07:52
To raise an exception, you use the raise statement with an argument that is either a class (which should subclass Exception for the "catch clause") or an instance.
>>> raise ExceptionTraceback (most recent call last):File "<stdin>", line 1, in ?Exception>>> raise Exception('hyperdrive overload')Traceback (most recent call last):File "<stdin>", line 1, in ?Exception: hyperdrive overload
2)custom Exception class
be sure to subclass Exception (either directly or indirectly, which means that subclassing any other built-in exception is okay). Thus, writing a custom exception basically amounts to something like this:
class SomeCustomException(Exception): pass
3) catch exception
1 more than one except clause
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/yexcept ZeroDivisionError: print "The second number can't be zero!"except TypeError: print "That wasn't a number, was it?"
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/yexcept (ZeroDivisionError, TypeError, NameError): print 'Your numbers were bogus...'
3 catch the object
If you want access to the exception object itself in an except clause, you can use two arguments instead of one. (Note that even when you are catching multiple exceptions, you are supplying except with only one argument—a tuple.) Because you explicitly catch the object itself, you can print it out so the user can see what happened.
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/yexcept (ZeroDivisionError, TypeError), e: print e
4) catch all vs catch most
If you do want to catch all exceptions in a piece of code, you can simply omit the exception class from the except clause:
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/yexcept: print 'Something wrong happened...'
try: x = input('Enter the first number: ') y = input('Enter the second number: ') value = x/y print 'x/y is', valueexcept Exception, e: print 'Invalid input:', e
5) "else" and "finally" statement
In some cases, it can be useful to have a block of code that is executed unless something bad happens; as with conditionals and loops, you can add an else clause to the try/except statement:
while True: try: x = input('Enter the first number: ') y = input('Enter the second number: ') value = x/y print 'x/y is', value except: print 'Invalid input. Please try again.' else: breakFinally, there is the finally clause. the finally clause will be executed, no matter what exceptions occur in the try clause. You can use it to do housekeeping after a possible exception. It is combined with a try clause:
x = Nonetry: x = 1/0finally: print 'Cleaning up...' del x
- About Exception
- About exception
- About Exception
- Java Tips -- About Exception
- About exception of Java
- about Exception and soapSeralization
- About Exception Handling
- About Java Exception
- [c#] About exception
- About Concurrent Modification Exception
- [ios] compile exception about ARC
- 简述java中的自定义异常的写法----about My Exception
- About Exception "ORA-01000: maximum open cursors exceeded"
- about:
- about
- About
- About
- About
- 列出所有子集
- 获取文件路径
- 分析笔记
- 【Android 开发】:UI控件之单选按钮 RadioButton 的使用方法
- JPype:实现在python中调用JAVA
- About Exception
- 树、森林和二叉树的转换
- 安装fedora18到win8上
- InputStream 、 InputStreamReader 、 BufferedReader
- TP重写模式
- spdylay 0.3.7 发布,SPDY 的 C 语言实现
- Struts2学习笔记03
- ORACLE 分析函数
- CPU处理器架构