Capture all exceptions from java-10.4
This section discusses how to capture all exceptions.
In fact, it is very easy to catch all exceptions. You only need to catch exceptions directly.
public class Test {public static void main(String[] args) {try {throw new Exception(cause);} catch (Exception e) {}}}
However, there may be many exceptions in complicated code, so it is inappropriate to put them in a code block.
The Exception of the custom or system-defined Exception-based subclass must be placed before the Exception. Otherwise, an error will be reported.
package com.ray.ch10;public class Test {public static void main(String[] args) {try {throw new SimpleException(cause);} catch (SimpleException e) {} catch (Exception e) {}// catch (SimpleException ex) {//error//// }}}class SimpleException extends Exception {public SimpleException(String msg) {super(msg);}}
From the code above, we can see that if we put SimpleException behind Exception, an Exception will be thrown.
The following describes some common exceptions.
package com.ray.ch10;public class Test {public static void main(String[] args) {try {throw new Exception(cause);} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());System.out.println(e.getLocalizedMessage());e.printStackTrace(System.out);}}}
Output:
Cause
Cause
Java. lang. Exception: cause
At com. ray. ch10.Test. main (Test. java: 6)
Java. lang. Exception: cause
At com. ray. ch10.Test. main (Test. java: 6)
The first two methods mainly obtain the information we placed. The last method mainly changes the method and position of the output stream.
Summary: This chapter describes how to capture all exceptions and how to place exceptions based on exceptions.