Lecture 6: Specification
来源:互联网 发布:mysql 查看连接密码 编辑:程序博客网 时间:2024/06/10 01:43
1 Specification
1.1 Specification structure
specification of a method consists of several clauses:
- a precondition , indicated by the keyword requires, is an obligation on the client.
- a postcondition , indicated by the keyword effects, is an obligation on the implementer.
1.2 Specifications in Java
preconditions into @param where possible, and postconditions into @return and @throws:
/** * Find a value in an array. * @param arr array to search, requires that val occurs exactly once * in arr * @param val value to search for * @return index i such that arr[i] = val */static int find(int[] arr, int val)
1.3 Null references
- null values are implicitly disallowed in parameters and return values.
- try to avoid null as much as possible.
1.4 Specifications for mutating methods
- a method should not modify its parameter.
- parameter could be declared with final key word.
- when writing a specification, use the convention that mutation is disallowed unless stated otherwise.
2 Exceptions
2.1 Exceptions for signaling bugs
- ArithmeticException , thrown for arithmetic errors like integer division by zero.
- NumberFormatException , thrown by methods like Integer.parseInt if you pass in a string that cannot be parsed into an integer.
2.2 Exceptions for special results
2.3 Checked and unchecked exceptions
Checked exceptions are called that because they are checked by the compiler:
- If a method might throw a checked exception, the possibility must be declared in its signature.
- If a method calls another method that may throw a checked exception, it must either handle it, or declare the exception itself, since if it isn’t caught locally it will be propagated up to callers.
- As a result, for an unchecked exception the compiler will not check for try - catch or a throws declaration.
2.4 Throwable hierarchy
- RuntimeException , Error , and their subclasses are unchecked exceptions. The compiler doesn’t require them to be declared in the throws clause of a method that throws them, and doesn’t require them to be caught or declared by a caller of such a method.
- All other throwables — Throwable , Exception , and all of their subclasses except for those of the RuntimeException and Error lineage — are checked exceptions. The compiler requires these exceptions to be caught or declared when it’s possible for them to be thrown.
2.5 Exception design considerations
- Exceptions are not just for signaling bugs. They can be used to improve the structure of code that involves procedures with special results.
- You should use an unchecked exception only to signal an unexpected failure (i.e. a bug), or if you expect that clients will usually write code that ensures the exception will not happen, because there is a convenient and inexpensive way to avoid the exception;
- Otherwise you should use a checked exception.
Reference
[1] 6.005 — Software Construction on MIT OpenCourseWare | OCW 6.005 Homepage at https://ocw.mit.edu/ans7870/6/6.005/s16/
0 0
- Lecture 6: Specification
- Lecture 6
- CS224d lecture 6札记
- Lecture 6 Order Statistics
- CS107-Lecture 6-Note
- Lecture 6 指针(上)
- Lecture 6 数组(中)
- Lecture 6 指针 (下)
- MIT 6.006 Algorithm Lecture 6
- Unit 1-Lecture 6:Sets and Sequence
- OpenCL 1.0 Specification阅读笔记(6)
- 2016/6/3 1005. Template Specification
- Stanford ML - Lecture 6 - Advice for applying machine learning
- Jordan Lecture Note-6: The Solutions of Nonlinear Equation.
- MIT 6.00 1x Lecture 6 Objects 对象 学习笔记
- 【CS231n】Lecture 6:Training Neural Networks,Part 2
- Lecture 6: Bisection methods, Newton/Raphson, introduction to lists
- Lecture Slides
- 数字排序转变为字母排序
- Spring的编码过滤器CharacterEncodingFilter
- SDM(superviseddescentmethod)人脸特征点训练
- 【JZOJ4161】于神之怒 莫比乌斯反演
- C语言实现http的下载
- Lecture 6: Specification
- Quartz
- 北航计算机机试C12年小岛面积
- iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇
- Linux下Redis的安装和部署
- 如何更改Docker容器的默认网桥docker0连接
- iOS开发深入了解宏定义#define
- 如何禁掉Windows 10上的所有广告
- 架构重构改善既有代码的设计