1Z0-829 Handling Exceptions • Complete Question Bank
Complete 1Z0-829 Handling Exceptions question bank — all 0 questions with answers and detailed explanations.
Refer to the exhibit.
```
public class ExceptionDemo {
public static void main(String[] args) {
try {
throw new RuntimeException("A");
} catch (RuntimeException e) {
throw new RuntimeException("B");
} finally {
throw new RuntimeException("C");
}
}
}
```You are responsible for a Java 17 application that processes user uploads. The application uses a custom AutoCloseable resource, UploadSession, which must always be closed after use to free server resources. A junior developer wrote the following code:
``` UploadSession session = new UploadSession(); try { session.upload(data); // more processing
} catch (UploadException e) {log.error("Upload failed", e);
} finally {session.close();
}
```
During a code review, you notice that the close() method of UploadSession throws a checked CloseException. The current code does not handle this exception. Which course of action should you recommend to ensure the application is robust and follows best practices?
Refer to the exhibit.
```
Exception in thread "main" java.lang.RuntimeException: Error processing order
at com.example.OrderService.process(OrderService.java:25)
at com.example.OrderService.main(OrderService.java:10)
Caused by: java.sql.SQLException: Connection timeout
at com.example.db.DatabaseConnection.connect(DatabaseConnection.java:15)
at com.example.OrderService.process(OrderService.java:22)
... 1 more
```You are developing a microservice that processes financial transactions. The service reads transaction data from a message queue, validates it, and writes results to a database. The code uses a try-with-resources statement to manage database connections. During testing, you notice that when a transaction fails validation, the service throws an IllegalStateException before closing the database connection, causing a resource leak. You need to ensure that the database connection is always closed properly, even if an exception is thrown during validation. The current code structure is:
try (Connection conn = DriverManager.getConnection(url, user, pass)) { // read from queue // validate transaction // if invalid, throw new IllegalStateException("Invalid transaction"); // write to database
}
Which course of action would you recommend?
Refer to the exhibit.
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic");
throw e;
} catch (Exception e) {
System.out.println("Exception");
} finally {
System.out.println("Finally");
}
}
}Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
String
int
long
boolean
java.sql.Timestamp
Consider the following code snippet:
try { // some code
} catch (IOException e) {// handle
} catch (FileNotFoundException e) { // Line X// handle
}
What will be the result?
Consider the following code:
public void process() throws Exception {try { riskyMethod();
} catch (IOException | SQLException e) {throw e;
} }
Assuming riskyMethod() declares both IOException and SQLException, what is the result?
Refer to the exhibit. Compilation output: Error: unreported exception java.io.IOException; must be caught or declared to be thrown at Test.main(Test.java:5)
Refer to the exhibit. Execution output: Exception in thread "main" java.io.IOException: IO error at Test.main(Test.java:10) Suppressed: java.io.IOException: Close error at Test$MyResource.close(Test.java:20)
Refer to the exhibit.
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
Usage:
throw new CustomException("Error");Given the following code snippet inside a method:
try { // risky code
} catch (IOException | SQLException e) {// handle
}
What is the implicit type of the exception variable 'e' in the catch block?
Refer to the exhibit.
try (FileInputStream fis = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(fis)) {
// read data
} catch (IOException e) {
System.out.println(e.getMessage());
Throwable[] suppressed = e.getSuppressed();
}Refer to the exhibit.
public class ExceptionDemo {
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println("Caught in main: " + e.getClass().getSimpleName());
}
}
static void methodA() throws IOException {
try {
throw new IOException();
} catch (Exception e) {
throw e;
}
}
}