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

后端 未结 6 1075
[愿得一人]
[愿得一人] 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:19

    This is exactly the situation where you should let the exception propagate up the call stack (declaring the method as throws SQLException or wrapping it in a application-specific exception) so that you can catch and handle it at a higher level.

    That's the whole point of exceptions: you can choose where to catch them.

提交回复
热议问题