Capture all exceptions from java-10.4

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.