我最近写了下面的代码; 它使用了很多异常处理.我认为它使代码看起来非常难以理解.我可以通过捕获泛型异常来缩短代码,例如
catch (Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但我也听说过捕获一般异常不是一个好的编码实践.
public class DataAnalyzerTester {
/**
* @param args args[0] stores the filename
* @exception NoSuchElementException if user attempts to access empty list element
* @exception ArithmeticException if user attempts to divide by 0
* @exception ArrayIndexOutOfBoundsException if user supplied less than 3 arguments
* @exception IOException problems with creating and writing files
* @exception RuntimeException if user attempts to pass empty list to constructor
*/
public static void main(String[] args) {
try{ …
Run Code Online (Sandbox Code Playgroud) 我有以下关系。
Suppliers( sid: integer, sname: string, address: string)
Parts(pid: integer, pname: string, color: string)
Catalog( sid: integer, pid: integer, cost: real)
Run Code Online (Sandbox Code Playgroud)
问题要求我找到 Acme Widget Suppliers 提供的零件的 pnames,而不是其他任何人。我写了下面的SQL语句;但是我觉得这个查询由于重复而效率低下。我想知道是否有更好的方法来编写此查询而不重复选择目录部分。
Select P.pname
FROM Parts P
WHERE P.pid IN (
Select C.pid
FROM Catalog C
INNER JOIN Supplier S
ON S.sid = C.sid
WHERE S.sname = "Acme Widget Suppliers"
AND C.pid NOT IN (
SELECT C2.pid
FROM Catalog C2
INNER JOIN Supplier S
ON S.sid = C2.sid
WHERE S.sname <> "Acme Widget Suppliers"
) …
Run Code Online (Sandbox Code Playgroud)