6-1 jmu-Java-06异常-finally
System.out.println("resource open success");
}
catch (Exception e)
{
System.out.println(e);
}
try
{
resource.close();
System.out.println("resource release success");
}
catch (Exception e)
{
System.out.println(e);
}
6-2 jmu-Java-06异常-多种类型异常的捕获
catch (Exception e) {
if(choice.equals("number"))
{
System.out.println("number format exception");
}
if(choice.equals("illegal"))
{
System.out.println("illegal argument exception");
}
if(choice.equals("except"))
{
System.out.println("other exception");
}
System.out.println(e);
}
6-3 jmu-Java-06异常-ArrayIntegerStack异常改进版
public Integer push(Integer item) throws FullStackException {
if(this.size() >= this.arrStack.length )throw new FullStackException();;
if (item == null)
return null;
this.arrStack[this.top] = item;
this.top++;
return item;
}//如果item为null,则不入栈直接返回null。如果栈满,抛出`FullStackException`。
public Integer pop() throws EmptyStackException {
if(this.empty()) throw new EmptyStackException();
if (this.arrStack[top-1] == null)
return null;
this.top--;
return this.arrStack[top];
}//出栈。如果栈空,抛出EmptyStackException。否则返回
public Integer peek() throws EmptyStackException {
if(top == 0) throw new EmptyStackException();
if (arrStack[top-1] == null) return null;
return arrStack[top-1];
}//获得栈顶元素。如果栈空,抛出EmptyStackException。
6-4 异常:圆半径不能为负数
class NumRangeException extends Exception
{
double r;
public NumRangeException(double r) {
this.r = r;
}
public void print()
{
System.out.printf("错误:圆半径%.1f为负数",r);
}
}
class Circle
{
double r;
public Circle(double r) {
this.r = r;
}
public double area() throws NumRangeException
{
if(r < 0) throw new NumRangeException(r);
return r*r*3.14;
}
}
6-5 异常:物品安全检查
class DangerException extends Exception
{
String message;
DangerException()
{
super("属于危险品!");
}
}
class Goods
{
private String name;
private boolean isDanger;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isDanger() {
return isDanger;
}
public void setDanger(boolean danger) {
isDanger = danger;
}
}
class Machine
{
void checkBag(Goods goods) throws DangerException
{
if(goods.isDanger() == true)
throw new DangerException();
}
}
6-6 求圆面积自定义异常类
class CircleException extends Exception
{
double r;
public CircleException(double r)
{
super();
this.r = r;
}
void print()
{
System.out.println("圆半径为"+this.r+"不合理");
}
}
class Circle
{
double r;
public Circle(double r) {
this.r = r;
}
double area() throws CircleException
{
if(this.r < 0)
throw new CircleException(r);
return 3.14*r*r;
}
}
7-1 jmu-Java-06异常-01-常见异常
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a[] = new int[5];
while(true)
{
String str = cin.next();
if(str.equals("arr"))
{
int num = cin.nextInt();
if(num > 4 || num < 0)
System.out.println("java.lang.ArrayIndexOutOfBoundsException: "+num);
}
else if(str.equals("num"))
{
String num = cin.next();
char c = num.charAt(0);
if(c > 57 || c < 48)
System.out.println("java.lang.NumberFormatException: For input string: \""+c+"\"");
}
if(str.equals("null"))
{
System.out.println("java.lang.NullPointerException");
}
else if(str.equals("cast"))
{
System.out.println("java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer");
}
else if(str.equals("other"))
return;
}
}
}
7-2 jmu-Java-06异常-02-使用异常机制处理异常输入
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[] a = new int[n];
int cnt = 0;
for(int i = 0; i < n; i ++)
{
try
{
String s = cin.next();
a[i] = Integer.parseInt(s);
} catch (Exception e) {
System.out.println(e);
i --;
}
}
System.out.println(Arrays.toString(a));
}
}
7-3 jmu-Java-06异常-04-自定义异常(综合)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String order = in.next();
while(!order.equals("other")) {
String name = in.next();
int score=0;
try {
Student aStudent = new Student();
score = in.nextInt();
aStudent.setName(name);
aStudent.addScore(score);
System.out.println(aStudent);
} catch (Exception e) {
if(e instanceof IllegalScoreException||e instanceof IllegalNameException) {
System.out.println(e);
}
else {
System.out.println("java.util.NoSuchElementException");
}
}
order = in.next();
}
in.close();
System.out.println("scanner closed");
}
}
class IllegalScoreException extends Exception{
public IllegalScoreException(){
}
public IllegalScoreException(String s) {
super(s);
}
}
class IllegalNameException extends Exception{
public IllegalNameException(){
}
public IllegalNameException(String s) {
super(s);
}
}
class Student
{
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) throws IllegalNameException {
if(name.charAt(0) >= '0' && name.charAt(0) <= '9')
throw new IllegalNameException("the first char of name must not be digit, name=" + name);
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int addScore(int score) throws IllegalScoreException {
int sum = this.score + score;
if(sum < 0 ||sum >100)
throw new IllegalScoreException("score out of range, score=" + score);
this.score += score;
return this.score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
}
7-4 天不假年
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int age;
age = in.nextInt();
Person p = new Person(age);
age = in.nextInt();
try{
p.setAge(age);
}catch(AgeException e){
System.out.println(e.getMessage());
}
}
}
class Person{
int age;
public Person(int age){
this.age = age;
}
public void setAge(int age) throws AgeException {
if(this.age <=age){
this.age = age;
System.out.println("A");
}else{
throw new AgeException("B");
}
}
}
class AgeException extends Exception{
public AgeException(String message)
{
super(message);
}
}
7-5 成绩录入时的及格与不及格人数统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int ji = 0, bu = 0;
while(n-- != 0)
{
int x = cin.nextInt();
if(x > 100 || x < 0)
{
System.out.println(x + "invalid!");
n ++;
continue;
}
if(x < 60) bu ++;
else ji ++;
}
System.out.println(ji);
System.out.println(bu);
}
}
7-6 较为复杂情况下的求和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int sum = 0;
String s = cin.nextLine();
String t[] = s.split(" ");
for(int i = 0; i < t.length; i ++)
{
try
{
sum += Integer.parseInt(t[i]);
}catch (Exception e)
{
continue;
}
}
System.out.println(sum);
}
}