栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

常用API、异常机制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

常用API、异常机制

1 Biginteger 1.1概述

1 Integer类是int的包装类,能存储的最大整型值为231-1,,long类也是有限的,最大值为263-1.如果要表示再大的整数,不论是基本数据类型还是他们的包装类 都无能为力,

2 java.math包的Biginteger可以表示不可变的任意精度的整数.

Biginteger的一些使用

package Bigintrger;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Biginteger_01 {
	public static void main(String[] args){
		//必须传入字符串
		BigInteger v1 = new BigInteger("123123");
		BigDecimal v2 = new BigDecimal(20);
		BigDecimal v3 = new BigDecimal(20);
		//+
		BigDecimal result = v2.add(v3);
		System.out.println(result);
		//-
		result = v2.subtract(v3);
		System.out.println(result);
		/
public class Exception_01 {
	public static void main(String[] args){
		System.out.println("---------");
		//除数不能为0
		int a = 10;
		int b = 0;
		if (b!=0){
			int c = a/b;
		}else{
			System.out.println("除数不能为零");
		}
		System.out.println("=======");
	}

}

 

package java_exception;

import java.io.FileInputStream;




public class Exception_02 {
	public static void main(String[] args){
		try{
			//加载对应文件
			FileInputStream fis = new FileInputStream("123.text");
			System.out.println("======");		
		}catch(Exception e){
			//打印错误栈帧和信息,一般用于程序员排错
			e.printStackTrace();
			//获取错误信息位置,一般用于反馈给客户端
			String msg = e.getMessage();
			System.out.println(msg);
			System.out.println("xxxxxx");
		}
		//不在终止生命周期执行
		System.out.println(1111);
	}

}
public class Exception_03 {
	public static void main(String[] args){
		try{
			m1();
		}catch(Exception e){
			//e.printStackTrace();
			//TODO解决错误的措施
		}
		System.out.println("====");
	}// throws 可以同时抛出多个异常,多个用逗号隔开
	public static void m1() throws FileNotFoundException,Exception {
		m2();
	}

	public static void m2() throws FileNotFoundException {
		m3();
	}

	public static void m3() throws FileNotFoundException {
		new FileInputStream("123");
	}

}
public class Exception_04 {
	public static void main(String[] args) {
		// 当try下面 可能会出现多个异常的时候,并且每个异常对应的解决方案不同的时候
		// 需要写多个catch进行不同的处理
		// 如果解决方案一致,则只写一个 exception 即可
		try {
			new FileInputStream("123");
			String stre = null;
			System.out.println(stre.trim());
		} catch (FileNotFoundException e) {
			System.out.println("找不到指定文件");
		} catch (IOException e) {
		} catch (NullPointerException e) {
			System.out.println("不能为空");
		} catch (Exception e) {
			System.out.println("其他异常");
		}
	}

}
public class Exception_05 {
	public static void main(String[] args) {
		try {
			new FileInputStream("123");
		} catch ( FileNotFoundException | NullPointerException e) {
			// TODO: handle exception
		}
	}
}
public class Exception_06 {
	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream("123");
			System.out.println(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			System.out.println("必须执行");
		}
	}
}
public class Exception_07 {
	public static void main(String[] args) {
		int result = m1();
		// 11
		System.out.println(result);
	}

	public static int m1() {
		int i = 10;
		try {
			// 因为finally中有return,所以这里的return 不执行,但是 i++执行,编译之后 会把return去掉
			return i++;
		} catch (Exception e) {
		} finally {
			System.out.println(i + "-------");
			return i;
		}
	}
}
public class Exception_08 {
	public static void main(String[] args) {
		// 提升作用域,否则访问不到
		// 添加默认值,否则因为局部变量没有默认值而报错
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D://123.txt");
			System.out.println("执行成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				// 判断是否打开该资源
				if (fis != null) {
					System.out.println("关闭成功");
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
public class Exception_09 {
	public static void main(String[] args) {
		try(
				FileInputStream fis = new FileInputStream("123");
				) {
			// 操作
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
public class Exception_10 {
	public static void main(String[] args) {

	}
}
class A {
	public void m1() throws IOException {

	}
}
class B extends A {
	public void m1() throws IOException {

	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709131.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号