当前位置:网站首页>2022/07/30 学习笔记 (day20) 面试题积累
2022/07/30 学习笔记 (day20) 面试题积累
2022-07-30 21:42:00 【激进的黄瓜】
目录
4.三个字符串String StringBuffer StringBuilder的区别 :
随时学,随时加
1.throws和throw的区别:
throws:在方法后边声明异常,其实就是自己不想对异常做出任何的处理,告诉别人自己可能出现的异常,交给别人处理,然别人处理
package com.xinkaipu.Exception;
class Math{
public int div(int i,int j) throws Exception{
int t=i/j;
return t;
}
}
public class ThrowsDemo {
public static void main(String args[]) throws Exception{
Math m=new Math();
}
}
throw: 就是自己处理一个异常,有两种方式要么是自己捕获异常try...catch代码块,要么是抛出一个异常(throws 异常)
package com.xinkaipu.Exception;
public class TestThrow
{
public static void main(String[] args)
{
try
{
//调用带throws声明的方法,必须显式捕获该异常
//否则,必须在main方法中再次声明抛出
throwChecked(-3);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
//调用抛出Runtime异常的方法既可以显式捕获该异常,
//也可不理会该异常
throwRuntime(3);
}
public static void throwChecked(int a)throws Exception
{
if (a > 0)
{
//自行抛出Exception异常
//该代码必须处于try块里,或处于带throws声明的方法中
throw new Exception("a的值大于0,不符合要求");
}
}
public static void throwRuntime(int a)
{
if (a > 0)
{
//自行抛出RuntimeException异常,既可以显式捕获该异常
//也可完全不理会该异常,把该异常交给该方法调用者处理
throw new RuntimeException("a的值大于0,不符合要求");
}
}
}2.代码块执行顺序:
代码块执行顺序:静态代码块——> 构造代码块 ——> 构造函数——> 普通代码块
继承中代码块执行顺序:父类静态块——>子类静态块——>父类代码块——>父类构造器——>子类代码块——>子类构造器
3.try ... catch、finally执行顺序:
finally永远是在最后执行的
如果在try ... catch语句中有return,
catch可以写多个异常(可以捕获多个异常)
顺序问题:先写小的,再写大的
4.三个字符串String StringBuffer StringBuilder的区别 :
边栏推荐
猜你喜欢
随机推荐
LeetCode·23.合并K个升序链表·递归·迭代
Knowledge of C language corners of byte alignment
拿什么来保护数据安全?基层数据安全体系建设待提升
Detailed explanation of the delete problem of ClickHouse delete data
【高等数学】矩阵与向量组的秩和等价
Markdown的使用
设备树的引入与体验
3分钟带你了解微信小程序开发
你需要知道的ES6—ES13开发技巧
不用bs4的原因居然是名字太长?爬取彩票开奖信息
socket: Kernel initialization and detailed process of creating streams (files)
基于ABP实现DDD--仓储实践
MySQL 8.0.29 set and modify the default password
(7/29)基础板子最小生成树prim+kruskal
冲刺第六周
1064 Complete Binary Search Tree
折叠旧版应用程序
HCIP第十六天
Be careful with your dictionaries and boilerplate code
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)










