A topic about Java exception handling _java

Source: Internet
Author: User
Tags exception handling getmessage

1, the establishment of exception package, writing Testexception.java program, the main method has the following code, to determine the possible exceptions, for capture processing.

public class Yichang {public
static void Main (string[] args) {for
(int i=0;i<4;i++) {
int k;
switch (i) {case
0:int zero=0; 
try{
K=911/zero;
} catch (ArithmeticException e) {
System.out.println ("There is an arithmetic exception!") ");
}
break;
Case 1: 
try{
int b[]=null;
k = b[0];
} catch (NullPointerException e) {
System.out.println ("NULL pointer exception occurred!") ");
}
break;
Case 2:
int c[]=new int[2];
try{
k=c[9];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("An array ordinal overflow occurred!") ");
}
break;
Case 3:
try{
char ch= "abc". CHARAT ()
catch (Stringindexoutofboundsexception e) {
System.out.println ("Data type conversion exception occurred!") ");
}
Break;}}}

2, set up a exception package, set up a bank class, the class has variable double balance means deposit, Bank class construction method can increase deposit, bank class has the method of withdrawing money withdrawal (double damount), When the amount of withdrawals is greater than the deposit, throw insufficientfundsexception, withdraw the amount of negative, throw nagativefundsexception, such as the new Bank (100), said deposited in the Bank 100 Yuan, Withdrawal (-15) is thrown out of the definition exception when using the method withdrawal.

public class Insufficientfundsexception extends Exception {public
String getMessage () {return
"your balance is insufficient!" ";
}
} 
public class Nagativefundsexception extends exception{public
String getMessage () {return
"the withdrawal amount cannot be negative!" ";
}
} 
public class Bank {
private static double balance;
Bank () {
};
Bank (double balance) {
this.balance=balance
}
public static void withdrawal (double damount) throws insufficientfundsexception,nagativefundsexception{
if ( damount>balance) {
throw new insufficientfundsexception ();
}
if (damount<0) {
throw new nagativefundsexception ();
}
}
public static void Main (string[] args) {
Bank b=new Bank;
System.out.println ("I have +balance+" Yuan deposit!) ");
try{
withdrawal ();
catch (Insufficientfundsexception | Nagativefundsexception e) {
e.printstacktrace ();
}
try{
withdrawal ( -15);
} catch (Nagativefundsexception | Insufficientfundsexception e) {
e.printstacktrace ();}}} 

Together on the topic of Java exception handling to introduce so many people, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for your support cloud Habitat community site!

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.