一.异常
1.概念
程序的非正常执行。高级语言都有异常处理机制(C++,Java)
2.一般处理异常的方法
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
String s = sc.nextLine();
if (s.matches("[0-9]+")) {
int x = Integer.parseInt(s);
System.out.println("输入的数字是:" + x);
} else {
System.out.println("程序异常");
}
3.异常方式
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
String s = sc.nextLine();
if (s.matches("[0-9]+")) {
int x = Integer.parseInt(s);
System.out.println("输入的数字是:" + x);
} else {
System.out.println("程序异常");
}
4.异常机制
4.1 try
探测代码块是否有异常
try{
语句块...
}
4.2 catch
异常捕获
cacth(异常对象 e){
}
4.3 finally
有无异常一定执行。异常机制中
在Java中,finally块通常用于释放资源、执行清理操作或记录错误。finally块总是会在try或catch块之后执行,无论是否出现异常。如果try块中没有出现异常,finally块将在try块之后立即执行;如果try块中出现异常,finally块将紧随catch块执行。
4.4 throws
方法上,声明方法内有异常,异常类型
4.5 throw
抛出异常对象
throw new 异常();
public static void f() throws Exception{
int a = 1;
int b = 0;
if(b==0) throw new Exception("除0异常");
}
5.异常体系
6.自定义异常
package com.ffyc.exception;
public class MyException01 extends Exception{
public MyException01(String msg){
super(msg);
}
}
class MyException02 extends RuntimeException{
public MyException02(String msg){
super(msg);
}
}
class testMyException01{
public static void test01() throws MyException01{
throw new MyException01("自定义异常继承Exception");
}
public static void test02(){
throw new MyException02("自定义异常继承RuntimeException");
}
public static void main(String[] args) {
test02();
try {
test01();
} catch (MyException01 e) {
throw new RuntimeException(e);
}
}
}
7.图书系统
7.1借书
业务流程,电脑查编号,bookshell找书
package com.ffyc.service;
import com.ffyc.entity.Book;
//接口
public interface IBookService {
Book borrowBook(String title);
}
package com.ffyc.exception;
//实现
public class BookException extends RuntimeException{
public BookException(String msg){
super(msg);
}
}
package com.ffyc.entity;
import java.util.Date;
public class Book {
private String title;
private String author;
private String content;
private String status;
private Date borrowDate;
private double price;
public Book(){
}
public Book(String title, String author, String content, String status, Date borrowDate, double price) {
this.title = title;
this.author = author;
this.content = content;
this.status = status;
this.borrowDate = borrowDate;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getBorrowDate() {
return borrowDate;
}
public void setBorrowDate(Date borrowDate) {
this.borrowDate = borrowDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", content='" + content + '\'' +
", status='" + status + '\'' +
", borrowDate=" + borrowDate +
", price=" + price +
'}';
}
}
package com.ffyc.service.impl;
import com.ffyc.entity.Book;
import com.ffyc.exception.BookException;
import com.ffyc.service.IBookService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BookServiceImpl implements IBookService {
private List<Book> books = new ArrayList<Book>();
{
books.add(new Book("西游记","武建军","我的大学生活","在库",null,33.44));
}
public Book borrowBook(String title) {
Iterator<Book> it = books.iterator();
while (it.hasNext()){
Book book = it.next();
if (book.getTitle().equals(title) && book.getStatus().equals("在库")){
return book;
}
}
throw new BookException("图书已借出或丢失");
}
public static void main(String[] args) {
IBookService ibs = new BookServiceImpl();
Book book = ibs.borrowBook("西游记");
System.out.println(book);
}
}