forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiCatch.java
More file actions
41 lines (38 loc) · 1.24 KB
/
Copy pathMultiCatch.java
File metadata and controls
41 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package exception;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.sql.SQLException;
/**
* - MultiCatch block
* introduced in Java 7
* multiple exceptions can be caught in same catch block
* those must be unrelated class
*
* Note:
* It is illegal to reassign the exception inside multi-catch
* while it is allowed in single exception catch block
*
*
*/
public class MultiCatch {
private static void throwsExceptions() throws Exception { }
public static void main(String[] args) {
try {
throwsExceptions();
}
catch(IllegalArgumentException | SQLException e){
e.printStackTrace();
}
catch(FileNotFoundException | IOException e){ // DOES NOT COMPILE, must be unrelated exceptions
// FileNotFoundException extends IOException
}
catch (ArithmeticException | ClassNotFoundException e){
e = new RuntimeException(); // DOES NOT COMPILE, reassign not allowed in multi-catch
}
catch (Exception e)
{
e = new RuntimeException("Re-assigned exception"); // reassigning exception is allowed in single catch
}
}
}