Tag: try catch

Java返回值(在try / catch子句中)

大家。 我有一个关于java中返回值的新手问题。 这是我的代码。 @Override public long addDrugTreatment(long id, String diagnosis, String drug, float dosage) throws PatientNotFoundExn { try { Patient patient = patientDAO.getPatientByDbId(id); long tid = patient.addDrugTreatment(diagnosis, drug, dosage); Connection treatmentConn = treatmentConnFactory.createConnection(); Session session = treatmentConn.createSession(true, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(treatmentTopic); TreatmentDto treatment = null; ObjectMessage message = session.createObjectMessage(); message.setObject(treatment); producer.send(message); return tid; } […]

未报告的exceptionjava.lang.exception

未报告的exceptionjava.lang.exception:必须被捕获或声明为throw。 为什么会出现这个问题? 是否有一些简单的方法可以帮助解决这个问题? 我在我的java中应用此代码.. public byte[] encrypt(String message) throws Exception { MessageDigest md = MessageDigest.getInstance(“md5”); byte[] digestOfPassword = md.digest(“ABCDEABCDE” .getBytes(“utf-8”)); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); for (int j = 0, k = 16; j < 8;) { keyBytes[k++] = keyBytes[j++]; } SecretKey key = new SecretKeySpec(keyBytes, "DESede"); IvParameterSpec iv = new IvParameterSpec(new byte[8]); Cipher cipher […]

尝试/捕捉内部或外部function

关于使用try catch(最佳实践),我有一个非常基本的问题。 我有一个像这样的简单函数(DAO) public void addVehicle(Vehicle vehicle) { em.getTransaction().begin(); em.persist(vehicle); em.getTransaction().commit(); } 并在Web服务中使用DAOfunction: @WebMethod(operationName = “addVehicle”) public void addVehicle(Vehicle vehicle) { try { vehicleDAO.addVehicle(vehicle); System.out.print(“Vehicle added”); } } catch (Exception e) { e.printStackTrace(); } } 或者更好地使用DAO函数中的try / catch如下所示: public void addVehicle(Vehicle vehicle) { try { em.getTransaction().begin(); em.persist(vehicle); em.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } […]

Java从DAO中删除重复的try,catch,最后的样板

我有一个DAO类,有很多方法,有很多重复的代码: public void method1(…) { Connection conn = null; try { //custom code here } catch (SQLException e) { LOG.error(“Error accessing the database.”, e); throw new DatabaseException(); } catch (QueryNotFoundException e) { LOG.error(“Error accessing the database.”, e); throw new DatabaseException(); } finally { if (conn != null) connectionPool.returnConnection(conn); } public void method2(…) { Connection conn […]

应该抓住哪个Throwable的子类,哪个不应该?

API doc说从不捕获Throwable子类Error表示exception行为。 它是否意味着Error和Exception之间的隔离是告诉程序员应该捕获哪个子类,哪个不应该? 或者还有更多呢?

Java:Try-Catch-Continue?

假设我可以使用一组语句: try { String a = getProperty(“a”); String b = getProperty(“b”); String c = getProperty(“c”); } catch(Exception e) { } 现在,假设找不到属性b,函数抛出exception。 在这种情况下,我将如何继续或者可能将b设置为null而不必为每个属性编写try-catch块? 我的意思是,a,b,c存在,但有时候可能根本找不到抛出exception的东西。

try catch块的成本是多少?

好多了: if (condition) { try { //something } catch(SomeEx ex) {} } 而不是这个: try { if (condition) { //something } } catch(SomeEx ex) {} 当我进入try块时,JVM实际上做了什么? 编辑:我不想知道在第二个例子总是进去尝试…请回答这个问题。

Java 6中复合if /或与try / catch的成本

如果声明,我们目前有以下化合物…… if ((billingRemoteService == null) || billingRemoteService.getServiceHeader() == null || !”00″.equals(billingRemoteService.getServiceHeader().getStatusCode()) || (billingRemoteService.getServiceBody() == null) || (billingRemoteService.getServiceBody().getServiceResponse() == null) || (billingRemoteService.getServiceBody().getServiceResponse().getCustomersList() == null) || (billingRemoteService.getServiceBody().getServiceResponse().getCustomersList().getCustomersList() == null) || (billingRemoteService.getServiceBody().getServiceResponse().getCustomersList().getCustomersList().get(0) == null) || (billingRemoteService.getServiceBody().getServiceResponse().getCustomersList().getCustomersList().get(0).getBillAccountInfo() == null) || (billingRemoteService.getServiceBody().getServiceResponse().getCustomersList().getCustomersList().get(0).getBillAccountInfo().getEcpdId() == null)) { throw new WebservicesException(“Failed to get information for Account Number ” + accountNo); } return billingRemoteService.getServiceBody().getServiceResponse().getCustomersList().getCustomersList().get(0); […]

在Java中尝试/捕获

有人可以给我一个暗示为什么这个尝试和捕获不起作用? 它会抛出扫描程序exception,而不是打印我期望的消息。 import java.util.*; import java.io.*; import java.math.*; import javax.swing.*; public class Main { public static void main(String[] args) { Boolean test = true; while (test == true) { try { double x, y; String operator; Scanner scan = new Scanner(System.in); Scanner scan_2 = new Scanner(System.in); Scanner ScanOperator = new Scanner(System.in); System.out.println(” Enter a double […]

您在Java中如何以及在何处定义自己的Exception层次结构?

您在Java中如何以及在何处定义自己的Exception层次结构? 我的主要问题涉及必须定义Exception类的包位置。 我们是否为exception创建了一个特殊的包并将所有类放在其中?