In Java, how do I set a return type if an exception occurs?

后端 未结 6 1063
[愿得一人]
[愿得一人] 2021-02-14 10:37

hey all, I\'m new to Java and was wondering if I define a method to return a database object

like

import java.sql.*;

public class DbConn {

    public          


        
6条回答
  •  一整个雨季
    2021-02-14 11:20

    You should just eliminate your entire try/catch block and allow exceptions to propagate, with an appropriate exception declaration. This will eliminate the error that Eclipse is reporting, plus right now your code is doing something very bad: by catching and re-throwing all exceptions, you're destroying the original stack trace and hiding other information contained in the original exception object.

    Plus, what is the purpose of the line Class.forName("com.mysql.jdbc.Driver").newInstance();? You're creating a new mysql Driver object through reflection (why?) but you're not doing anything with it (why?).

提交回复
热议问题