今天是周一,和平常一样没什么特殊的事情发生。下午在闲鱼上买了一部小米5作为测试机,希望能不被坑,也就用这个来测试一下开发的应用罢了,没指望它能有多大的用途。今天学了JAVA的异常处理和io流的一部分知识,时而学习时而摸鱼?哎,还是感觉非常的尴尬啊,自己在那里一坐废物的一批。我和导师说自己写不出来那个APP,也不知道他到底听进去了吗。还让我写那个根本就没思路啊。不说了,想过了年就辞职去考雅思,也不知道家里会不会同意这件事情呢,说出来怕不是要被毒打。就这样,明天继续加油
package homework;
/**
* 假设有一个方法 public int method(), 会返回一个整数
* 在这个方法中有try catch 和 finally.
* try 里返回 1
* catch 里 返回 2
* finally 里 返回 3
* 那么,这个方法到底返回多少?
*/
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String[] args) {
System.out.println(method());
}
private static int method(){
try {
return 1;
} catch (Exception e) {
e.printStackTrace();
return 2;
}
finally {
return 3;
}
}
}Throwable是类,Exception和Error都继承了该类,所以在捕捉的时候,也可以使用Throwable进行捕捉
如图: 异常分Error和Exception,Exception里又分运行时异常和可查异常。
package homework;
/**
* 在方法声明上,可以抛出指定的异常,比如FileNotFoundException
* 那么能否抛出Throwable这个类?
*
* 这个方法的调用者又该如何处理?
*/
import java.io.File;
import java.io.FileInputStream;
public class Test {
public static void main(String[] args) {
try{
throwmethod();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private static void throwmethod() throws Throwable{
File f = new File("d:/LOL.exe");
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
}package homework;
/**
* 练习-异常综合1
*/
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public double getBalance(){
return balance;
}
public void deposit(double addBalance){
balance = balance + addBalance;
}
public void withdraw(double minusBalance) throws OverDraftException{
if (balance - minusBalance < 0){
throw new OverDraftException("余额不足", minusBalance - balance);
}
balance = balance - minusBalance;
}
}
package homework;
public class OverDraftException extends Exception {
//deficit为透支
private double deficit;
public double getDeficit() {
return deficit;
}
public OverDraftException(String msg, double deficit) {
super(msg);
this.deficit = deficit;
}
}
package homework;
public class Test {
public static void main(String[] args) {
//开户存了1000
Account a = new Account(1000);
//存钱1000
a.deposit(1000);
//查看余额
System.out.println(a.getBalance());
try {
//取2001
a.withdraw(2001);
} catch (OverDraftException e) {
System.err.println("透支金额:"+e.getDeficit());
e.printStackTrace();
}
}
}package homework;
/**
* 练习-异常综合2
*/
public class CheckingAccount extends Account {
//此属性代表透支额度
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance,double overdraftProtection){
super(balance);
this.overdraftProtection = overdraftProtection;
}
@Override
public void withdraw(double minusBalance) throws OverDraftException{
if (minusBalance > balance + overdraftProtection){
double deficit = minusBalance - balance - overdraftProtection;
throw new OverDraftException("透支额度超标", deficit);
}
balance = balance - minusBalance;
}
}
package homework;
public class Test {
public static void main(String[] args) {
//开户存了1000块,拥有500的透支额度
CheckingAccount a = new CheckingAccount(1000, 500);
//存了1000
a.deposit(1000);
//查询余额
System.out.println(a.getBalance());
try {
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
a.withdraw(600);
System.out.println(a.getBalance());
} catch (OverDraftException e) {
System.err.println("透支超额:" + e.getDeficit());
e.printStackTrace();
}
}
}
京公网安备 11010502036488号