10个Java函数,提供更好的错误处理
1. IllegalArgumentException
public static void addUser(String name, int age) {
if(name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
if(age < 0 || age > 120) {
throw new IllegalArgumentException("Invalid age");
}
// rest of the code
}
This function throws an IllegalArgumentException if the name is null or empty or if the age is not within the valid range of 0 to 120.
2. IllegalStateException
public static void startServer() {
if(serverRunning) {
throw new IllegalStateException("Server already running");
}
// start the server
serverRunning = true;
}
This function throws an IllegalStateException if the server is already running, preventing the code from starting it again.
3. NullPointerException
public static void printName(String name) {
if(name == null) {
throw new NullPointerException("Name cannot be null");
}
System.out.println(name);
}
This function throws a NullPointerException if the name is null, helping to prevent null references during runtime.
4. IndexOutOfBoundsException
public static void getItemAtIndex(int index) {
if(index < 0 || index >= list.size()) {
throw new IndexOutOfBoundsException("Invalid index");
}
return list.get(index);
}
This function throws an IndexOutOfBoundsException if the index is not within the valid range of the list, ensuring the code does not try to access an item that does not exist.
5. ConcurrentModificationException
public static void removeItemFromList(List<String> list, String item) {
Iterator<String> it = list.iterator();
while(it.hasNext()) {
String current = it.next();
if(current.equals(item)) {
it.remove();
}
else {
throw new ConcurrentModificationException("List has been modified during iteration");
}
}
}
This function throws a ConcurrentModificationException if the list is modified during the iteration process, preventing any potential concurrent modification errors.
6. ClassCastException
public static void addItem(Object obj) {
MyType myType = (MyType) obj;
// rest of the code
}
This function throws a ClassCastException if the object passed in cannot be cast to the desired type, preventing any potential runtime errors.
7. IOException
public static void readFile(String fileName) throws IOException {
File file = new File(fileName);
if(!file.exists()) {
throw new IOException("File does not exist");
}
// rest of the code
}
This function throws an IOException if the file does not exist or if there is an issue reading the file, helping to handle file I/O errors.
8. SQLException
public static void insertRecord(String name, int age) throws SQLException {
Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (name, age) VALUES (?,?)");
stmt.setString(1, name);
stmt.setInt(2, age);
int rows = stmt.executeUpdate();
if(rows == 0) {
throw new SQLException("Failed to insert record");
}
// rest of the code
}
This function throws a SQLException if there is an issue inserting a record into the database, providing better error handling for database operations.
9. ArithmeticException
public static void divide(int num1, int num2) {
if(num2 == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
int result = num1 / num2;
// rest of the code
}
This function throws an ArithmeticException if the second number is zero, avoiding any potential division by zero errors.
10. UnsupportedOperationException
public static void removeFirstElement(List<String> list) {
throw new UnsupportedOperationException("Remove operation not supported");
}
This function throws an UnsupportedOperationException if the remove operation is not supported on the list, preventing any potential errors during runtime.
