Java Study Notes ---- exception

Source: Internet
Author: User

Java Study Notes ---- exception

This is a picture stolen from other websites. There are two types of exceptions: Error: An Error Exception that cannot be handled by the Program: Exception: throws, if a car fails, the car cannot handle it by itself, so it can only be handed over to someone to handle throw in the function body. This is a custom exception class. You can output your own defined exceptions. Note: This throw also requires throws MyException. At first, I thought it was unnecessary. So the error code is:

Package com. ehealth. exc; import java. lang. exception; public class ExcTest {static int question (int x, int y) throws MyException {if (y <0) {throw new MyException ("the divisor cannot be a negative number ");} return x/y;} static int que (int x, int y) throws MyException {if (x <0) {throw new MyException ("the first digit cannot be less than 0");} return x + y;} public static void main (String [] args) {// TODO Auto-generated method stub int x = 3; int y =-1; int I =-1; Int j = 1; try {int a = que (I, j);} catch (MyException e1) {// TODO Auto-generated catch block System. out. println (e1.getMessage ();} try {int res = question (x, y);} catch (MyException e) {System. out. println (e. getMessage ();} catch (ArithmeticException e) {System. out. println (e. getMessage ();} catch (Exception e) {System. out. println ("Other exceptions occurred in the program! ") ;}} Class MyException extends Exception {String message; public MyException (String Err) {message = Err;} public String getMessage () {return message ;}}

 

 

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.