错误处理 : 例外 « 开发相关 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 开发相关 » 例外 
6. 23. 1. 错误处理
  1. You can isolate code that may cause a runtime error using the try statement.
  2. try statement normally is accompanied by the catch and the finally statements.
  3. In case of an error, Java stops the processing of the try block and jump to the catch block.
  4. In the catch block, you can handle the error or notify the user by 'throwing' a java.lang.Exception object. Or you can re-throw the exception or a new Exception object back to the code that called the method.
  5. If a thrown exception is not caught, the application will stop abruptly.
  6. In the finally block, you write code that will be run whether or not an error has occurred.
  7. The finally block is optional.
  8. You can have more than one catch block. This is because the code can throw different types of exceptions.
  9. If the type of exception thrown does not match the exception type in the first catch block, the JVM goes to the next catch block and does the same thing until it finds a match.
  10. If no match is found, the exception object will be thrown to the method caller.
  11. If the caller does not put the offending code that calls the method in a try block, the program crashes.

This is the syntax of the try statement.

try {
         [code that may throw an exception]
     catch (ExceptionType-e) {
         [code that is executed when ExceptionType-is thrown]
     } [catch (ExceptionType-e) {

         [code that is executed when ExceptionType-is thrown]
     }]
       ...
     } [catch (ExceptionType-n e) {
         [code that is executed when ExceptionType-n is thrown]
     }]
     [finally {
         [code that runs regardless of whether an exception was thrown]]
     }]
6. 23. 例外
6. 23. 1. 错误处理
6. 23. 2. 例外类型
6. 23. 3. Java检查RuntimeException子类
6. 23. 4. Java's Checked Exceptions Defined in java.lang
6. 23. 5. 抛出一个异常的方法
6. 23. 6. catch块来处理java.lang.Exception
6. 23. 7. 例外处理
6. 23. 8. 多个catch
6. 23. 9. finally块
6. 23. 10. 例外对象:堆栈跟踪
6. 23. 11. 确定你自己的例外,使你自己的异常
6. 23. 12. 例外链接。
6. 23. 13. 使用堆栈跟踪的一个例外情况
6. 23. 14. Put printStackTrace() into a String: redirect the StackTrace to a String with a StringWriter/PrintWriter
6. 23. 15. 这个程序会创建一个自定义异常类型。
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.