day22文字笔记
正课内容:
1)finally
public class FinallyDemo {
public static void main(String[] args) {
System.out.println(“程序开始了!”);
try{
String str = “123”;
System.out.println(Integer.parseInt(str));
return;//finally真正执行之后,方法才会真正的返回
}catch (Exception e){
System.out.println(“出错了”);
}finally {
System.out.println(“finally中的代码执行了!”);
}
System.out.println("程序结束了!");
}
}
案例:
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(“fos.dat”);
fos.write(1);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2)finally,final,finalize区别
分别说明:final,finally,finalize
finalize方法是Object中定义的方法,该方法是一个对象生命周期中的最后一个方法、当GC即将释放一个对象前会调用该对象的
finalize方法,调用后该对象被释放。需要注意,若需要重写这个方法,里面不应当有耗时的操作,会影响GC的工作
*/
public class FinallyDemo3 {
public static void main(String[] args) {
System.out.println(
test(“0”)+","+test(null)+","+test("")
);
}
public static int test(String str){
try{
return str.charAt(0)-'0';
}catch (NullPointerException e){
return 1;
}catch (Exception e){
return 2;
}finally {
return 3;
}
}
}
3)异常的抛出
-
throw关键字允许我们主动对外抛出一个异常,通常下列情况会如此:
-
1:当前代码片段出现了一个异常,但是该异常不应当在当前代码片段中被解决时可对外抛出。
-
2:满足语法但是不满足业务逻辑时,可以将其当做异常抛出给调用者。
/
public class ThrowDemo {
public static void main(String[] args) {
Person p = new Person();
try {
/
当我们调用一个含有throws声明异常抛出的方法时,编译器要求我们必须
处理这个异常,处理方式有两种:
1:主动try-catch捕获并解决该异常
2:在当前方法上继续使用throws声明该异常的抛出
具体选取哪个要结合实际业务分析异常处理的责任制问题。
*/
p.setAge(10000);//满足语法,但是不满足业务场景
} catch (IllegalAgeException e) {
e.printStackTrace();
}System.out.println("此人年龄:"+p.getAge());}
}
4)
public class ThrowsDemo {
public void dosome()throws IOException, AWTException {}
}
class SubClass extends ThrowsDemo{
// public void dosome()throws IOException, AWTException {}
//允许不再抛出任何异常
// public void dosome(){}
//允许仅抛出部分异常
// public void dosome()throws IOException{}
//允许抛出超类方法抛出异常的子类型异常
// public void dosome()throws FileNotFoundException {}
//不允许抛出额外异常(超类方法没有抛出的且没有继承关系的异常)
// public void dosome()throws SQLException {}
//不允许抛出超类方法抛出异常的超类型异常。
// public void dosome()throws Exception {}
}
5)
public class ThrowsDemo {
public void dosome()throws IOException, AWTException {}
}
class SubClass extends ThrowsDemo{
// public void dosome()throws IOException, AWTException {}
//允许不再抛出任何异常
// public void dosome(){}
//允许仅抛出部分异常
// public void dosome()throws IOException{}
//允许抛出超类方法抛出异常的子类型异常
// public void dosome()throws FileNotFoundException {}
//不允许抛出额外异常(超类方法没有抛出的且没有继承关系的异常)
// public void dosome()throws SQLException {}
//不允许抛出超类方法抛出异常的超类型异常。
// public void dosome()throws Exception {}
}
6)
public class TryCatchDemo {
public static void main(String[] args) {
System.out.println(“程序开始了…”);
try {
// String str = null;
// String str = “”;
String str = “a”;
//当JVM执行程序遇到一个异常时就会实例化该异常并将其抛出
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(Integer.parseInt(str));
//try语句块中报错语句下面的内容都不会被执行!
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!");
// }catch(NullPointerException e){
// //try中出现空指针的解决办法,实际开发要结合需求来进行处理。
// System.out.println(“出现了空指针!”);
// //可以定义多个catch来捕获不同的异常并进行对应的处理
// }catch(StringIndexOutOfBoundsException e){
// System.out.println(“出现了字符串的下标越界”);
//当多个异常的解决办法相同时,可以使用同一个catch。
}catch(NullPointerException | StringIndexOutOfBoundsException e){
System.out.println("出现空指针或下标越界的统一解决办法!");
//可以在最后一个catch处捕获Exception,避免因为一个未捕获的异常导致程序中断
}catch(Exception e){
System.out.println("反正就是出了个错!");
}
System.out.println("程序结束了!");
}
}
7)
public class IllegalAgeException extends Exception{
public IllegalAgeException() {
}
public IllegalAgeException(String message) {
super(message);
}
public IllegalAgeException(String message, Throwable cause) {
super(message, cause);
}
public IllegalAgeException(Throwable cause) {
super(cause);
}
public IllegalAgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
8)
public class ExceptionApiDemo {
public static void main(String[] args) {
System.out.println(“程序开始了…”);
try {
String str = “abc”;
System.out.println(Integer.parseInt(str));
} catch (NumberFormatException e) {
//将当前错误信息输出到控制台
//输出错误信息与System.out.println()是并发的,因此控制台显示顺序可能不同
e.printStackTrace();
//获取错误消息,一般用于提示给用户或记录日志时使用
String message = e.getMessage();
System.out.println(message);
}
System.out.println("程序结束了...");
}
}
9) 聊天室客户端
public class Client {
private Socket socket;
public Client(){
try {
System.out.println("正在连接服务端...");
socket = new Socket("localhost",8088);
System.out.println("与服务端建立了连接!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
try {
OutputStream out = socket.getOutputStream();
//转换流(高级流),负责衔接字符与字节流,将写出的字符转换为字节
OutputStreamWriter osw = new OutputStreamWriter(out,"UTF-8");
//缓冲流(高级流),负责块写文本数据加速
BufferedWriter bw = new BufferedWriter(osw);
//PW(高级流)按行写出字符串,自动行刷新
PrintWriter pw = new PrintWriter(bw,true);
pw.println("你好服务端!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client();
client.start();
}
}
10)聊天室服务端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
private ServerSocket serverSocket;
public Server(){
try {
System.out.println(“正在启动服务端…”);
serverSocket = new ServerSocket(8088);
System.out.println(“服务端启动完毕!”);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
try {
System.out.println(“等待客户端连接…”);
Socket socket = serverSocket.accept();
System.out.println("一个客户端连接了!");
InputStream in = socket.getInputStream();
//转换流(高级流,字符流)。1:衔接字符与字节流 2:将读取的字节转换为字符
InputStreamReader isr = new InputStreamReader(in);
//缓冲流(高级流,字符流)。块读文本数据加速。按行读取字符串
BufferedReader br = new BufferedReader(isr);
//读取客户端发送的一行字符串
String message = br.readLine();
System.out.println("客户端说:"+message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.start();
}
}



