Java's catch all exceptions

We are in the process of learning the exception, you can only go to write an exception handler to catch all types of exceptions.

I.e., by capturing the base class Exception exception type you can do this:

catch(Exception e){
     System.out.println("Caught an exception");
}

In this way we will catch all exceptions, so it is best to deal with it at the end of the list of programs to prevent it ahead of other handlers to capture the first abnormal.

With a program to demonstrate the following:

public class ExceptionMethods {
	
	public static void main(String[] args) {
		try{
			throw new Exception("My Exception");
		}catch(Exception e){
			System.out.println("Caught Exception");
			System.out.println("getMessage():"+e.getMessage());
			System.out.println("getLocalizedMessage():"+
			                     e.getLocalizedMessage());
			System.out.println("toString()"+e);
			System.out.println("printStackTrace():");
			e.printStackTrace(System.out);
		}
	}
}

operation result:

This provides a reference for everyone!

Thank you!

Guess you like

Origin blog.csdn.net/qq_41026809/article/details/92001743