FileOutputStream out = null;
try {
out = new FileOutputStream("abc.text");
out.write(1);
System.out.println("写入成功");
} catch (FileNotFoundException e) {
System.out.println("要写入的文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("发生了IO错误");
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Travel {
private static int power = 100;
private static boolean bridgeIsOk = true;
public static void main(String[] args) {
//描述一下坐火车旅游的过程
System.out.println("从济南出发, 到北京旅游");
System.out.println("列车开到德州");
//中途给妈妈打个电话
try{
telToMom();
}catch(BatteryDiedException e){
System.out.println("换一块电池, 继续旅程");
}
//桥断了
if(!bridgeIsOk){
System.out.println("旅程结束");
throw new BridgeBreakError("桥断了,列车停止运行");
}
System.out.println("到北京站,下车");
//下雨了
try{
throw new RainException("下雨了");
}catch(RainException e){
System.out.println("撑起准备的雨伞, 继续旅程");
}
}
private static void telToMom() throws BatteryDiedException{
if(power == 0){ //手机电量为0
System.out.println("手机没电了");
throw new BatteryDiedException("手机没电了");
}
System.out.println("给妈妈打电话");
}
static class BatteryDiedException extends Exception{
public BatteryDiedException(String msg){
super(msg);
}
}
static class BridgeBreakError extends Error{
public BridgeBreakError(String msg){
super(msg);
}
}
static class RainException extends Exception{
public RainException(String msg){
super(msg);
}
}
}
public static Class<?> forName(String className)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getCallerClassLoader());
}
try {
Class clazz = Class.forName("com.bjpowernode.Person");
} catch (ClassNotFoundException e) {
System.out.println("Person类加载失败");
System.exit(0);
e.printStackTrace();
}
private static boolean isSick = true;
public static void main(String[] args) {
if(isSick){
System.out.println("生病了,旅途中止");
throw new SickException("病了");
}
}
private static class SickException extends RuntimeException{
public SickException(String msg){
super(msg);
}
}
public static void main(String[] args) {
caculateSalary(3);
}
/**
* 计算一个月的薪资
* @param month 月份
*/
public static void caculateSalary(int month){
//如果参数错误, 抛出非法参数异常
if(month < 1 || month > 12){
throw new IllegalArgumentException();
}
}
private static void caculateSalaryInner(int month){
//计算薪资 ...
}
public static void main(String[] args) {
doSomething();
}
public static void doSomething(){
Object obj = null;
try { //运行时异常也是可以捕获的
obj.toString();
} catch (RuntimeException e) {
System.out.println("抛出了运行时异常, 异常的具体类型:" + e.getClass().getName());
}
}
public static void main(String[] args) {
doSomething(); //不必处理方法声明抛出的运行时异常
}
public static void doSomething() throws RuntimeException{
throw new RuntimeException();
}
public class DoWork {
public static class Boss{ //老板
private Employee emp; //员工对象
public Boss(Employee emp){
this.emp = emp;
}
public void doWork(){
try {
emp.doWork(); //老板委托员工外出执行任务,给员工块钱的经费
} catch (TaskCannotCompleteException e) { //任务无法完成
System.out.println("派出另一个员工去完成任务");
}
}
}
public static class Employee{ //员工
//执行任务,可能不能完成任务
public void doWork(float money) throws TaskCannotCompleteException{
//
if(money < ){ //经费太少,无法执行任务
throw new MoneyNotEnoughException();
}
//
try {
goToWorkPlace();
} catch (CannotFindBusException e) { //在去工作地点时找不到公交车
System.out.println("打车去");
}
//
try {
workDayAndNight();
} catch (TiredToSickException e) { //累病了
//告诉老板,任务无法完成
throw new TaskCannotCompleteException();
}
}
//在去工作地点时可能找不到公交车
private void goToWorkPlace() throws CannotFindBusException{
//throw new CannotFindBusException();
}
//没天没夜的干活, 可能会累病
private void workDayAndNight() throws TiredToSickException{
//throw new TiredToSickException();
}
}
//找不到公交车异常
public static class CannotFindBusException extends Exception{}
//经费不足异常
public static class MoneyNotEnoughException extends RuntimeException{}
//累病异常
public static class TiredToSickException extends Exception{}
//任务无法完成异常
public static class TaskCannotCompleteException extends Exception{}
public static void main(String[] args) {
Boss boss = new Boss(new Employee());
boss.doWork();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有