java.lang包下的异常实例Exception如下:
Exception
-ClassNotFoundException
-CloneNotSupportedException
-IllegalAccessException
-InstantiationException
-InterruptedException
-NoSuchFieldException
-NoSuchMethodException
RuntimeException extends Exception
-ArithmeticException
-IndexOutOfBoundsException
-ArrayIndexOutOfBoundsException
-StringIndexOutOfBoundsException
-ArrayStoreException
-ClassCastException
-EnumConstantNotPresentException
-IllegalArgumentException
-IllegalThreadStateException
-NumberFormatException
-IllegalMonitorStateException
-IllegalStateException
-IndexOutOfBoundsException
-NegativeArraySizeException
-SecurityException
-NullPointerException
-TypeNotPresentException
Exception的描述如下:
以下给出异常产生的实例,红色标记的暂时还没有提供例子。
ArithmeticException
- 包 my.exception;
- 公共 类 ArithmeticExceptionTest {
- 公共 静态 无效的 主要(字串[] args){
- A = 10 ;
- = / 0 ;
- }
- }
ArrayIndexOutOfBoundsException异常
- 包 my.exception;
- 公共 类 ArrayIndexOutOfBoundsExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 诠释[]数组= { 1 ,2 ,3 };
- System.out.println(数组[ 3 ]);
- }
- }
ArrayStoreException信息
- 包 my.exception;
- 导入 的java.util.HashMap;
- 导入 的java.util.Map;
- 公共 类 ArrayStoreExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 地图<String,对象>地图= 新 的HashMap <String,对象>();
- map.put( “ 数字” , 123 );
- map.put( “ 字符” , “Java”的);
- [] valueArr = map.values 的()。对象的toArray(新 的String [map.size()]);
- ( I = 0 ; <valueArr.length;我+ +){
- System.out.println(valueArr []);
- }
- }
- }
时抛出
- 包 my.exception;
- 公共 类 ClassCastExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 对象x = 新的 整数(0 );
- System.out.println((弦乐)×);
- }
- }
ClassNotFoundException的的
包my.exception; 公共的类ClassNotFoundExceptionExample { 公共静态无效的主要(字串[]参数)抛出ClassNotFoundException的{ C类=调用Class.forName(的“my.exception.ClassNotFoundExceptionExample1”);} } CloneNotSupportedException异常
- 包 my.exception;
- 公共 类 CloneNotSupportedExceptionExample {
- 公共 静态 无效的 主要(字串[] args) 抛出 CloneNotSupportedException异常{
- CloneNotSupportedExceptionExample C2 =(CloneNotSupportedExceptionExample): 新 CloneNotSupportedExceptionExample()
- 克隆();
- }
- }
IllegalArgumentException异常
- 包 my.exception;
- 公共 类 IllegalArgumentExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- / /做工精细
- 季= Enum.valueOf(Season. 类, “春秋” );
- / /抛出IllegalArgumentException异常
- 赛季S1 = Enum.valueOf(Season. 类, “SPRING111” );
- }
- }
- 枚举 季节
- {
- 春,夏,秋,冬;
- }
IllegalMonitorStateException
- 包 my.exception;
- 公共 类 IllegalMonitorStateExceptionExample {
- 公共 静态 无效的 主要(字串[] args) 抛出 InterruptedException的{
- 主题测试= 新 主题();
- test.start();
- test.wait();
- test.countStackFrames();
- }
- }
IllegalThreadStateException
- 包 my.exception;
- 公共 类 IllegalThreadStateException {
- 公共 静态 无效的 主要(字串[] args){
- 主题测试= 新 主题();
- / /线程不能启动两次。如果我们这样做,那么一个IllegalThreadStateException发生。
- test.start();
- test.start();
- }
- }
InstantiationException
- 包 my.exception;
- 公共 类 InstantiationExceptionExample {
- 静态的 对象createNewInstance(Object obj)在{
- 尝试 {
- 返回的newInstance();
- } 赶上 (InstantiationException E){
- e.printStackTrace();
- } 赶上 (IllegalAccessExceptionÉ){
- e.printStackTrace();
- }
- 返回 NULL ;
- }
- 公共 静态 无效的 主要(字串[] args){
- 的String [] = 新 的String [] { “A” , “B” , “C” };
- createNewInstance(次);
- }
- }
InterruptedException的
- 包 my.exception;
- 公共 类 InterruptedExceptionExample {
- 公共 静态 无效的 主要(字串[] args) 抛出 InterruptedException的{
- 最终 线程T1 = 新 主题(){
- 公共 无效 的run(){
- 尝试 {
- 视频下载(10000 );
- } 赶上 (InterruptedException的E){
- e.printStackTrace();
- }
- }
- };
- 的线程T2 = 新 主题(){
- 公共 无效 的run(){
- 尝试 {
- 视频下载(2000年);
- } 赶上 (InterruptedException的E){
- e.printStackTrace();
- }
- t1.interrupt();
- }
- };
- t1.start();
- t2.start();
- }
- }
NegativeArraySizeException
- 包 my.exception;
- 公共 类 NegativeArraySizeExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- []数组= 新的 诠释- 1 ];
- }
- }
NoSuchFieldException
- 包 my.exception;
- 导入 java.lang.reflect.Field;
- 公共 类 NoSuchFieldException {
- 公共 静态 无效的 主要(字串[] args) 抛出 异常{
- 场场= NoSuchFieldException 类getfield命令(“姓名” );
- }
- }
的NoSuchMethodException
- 公共 类 NoSuchMethodExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 尝试 {
- NoSuchMethodExceptionExample 类getMethod方法(“的getName” ,弦乐类);
- } 赶上 (SecurityExceptionÉ){
- / / TODO自动生成的catch块
- e.printStackTrace();
- } 赶上 (的NoSuchMethodException E){
- / / TODO自动生成的catch块
- e.printStackTrace();
- }
- }
- }
NullPointerException异常
- 包 my.exception;
- 公共 类 NullPointerExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 字符串名称= NULL ;
- system.out.println(name.toLowerCase());
- }
- }
NumberFormatException异常
- 包 my.exception;
- 公共 类 NumberFormatExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 值= Integer.valueOf(“123.25f” );
- }
- }
StringIndexOutOfBoundsException的
包my.exception;
- 公共 类 StringIndexOutOfBoundsExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- 字符串值= “世界,你好!”
- 炭, C = value.charAt(20 );
- }
- }
操作UnsupportedOperationException
- 包 my.exception;
- 进口 java.util.Arrays;
- 进口 的java.util.List;
- 公共 类 UnsupportedOperationExceptionExample {
- 公共 静态 无效的 主要(字串[] args){
- String []数组= { “你好” ,“世界” ,“爪哇” };
- 列表<STRING>测试= Arrays.asList(数组);
- / / Arrays.asList获取的名单是不能用于删除的
- test.remove( “ 你好” );
- }
- }