java.lang包下的异常及例子

java.lang包下的Exception如下:

[b]Exception[/b]
-ClassNotFoundException
-CloneNotSupportedException
-[color=red]IllegalAccessException[/color]
-InstantiationException
-InterruptedException
-NoSuchFieldException
-NoSuchMethodException

[b]RuntimeException[/b] extends [b]Exception[/b]
-ArithmeticException
-IndexOutOfBoundsException
-ArrayIndexOutOfBoundsException
-StringIndexOutOfBoundsException
-ArrayStoreException
-ClassCastException
-[color=red]EnumConstantNotPresentException[/color]
-IllegalArgumentException
-IllegalThreadStateException
-NumberFormatException
-IllegalMonitorStateException
-[color=red]IllegalStateException[/color]
-IndexOutOfBoundsException
-NegativeArraySizeException
-[color=red]SecurityException[/color]
-NullPointerException
-[color=red]TypeNotPresentException[/color]

Exception的描述如下:

[img]http://dl2.iteye.com/upload/attachment/0088/3788/d56bd022-21b4-32e4-9380-9620306cee94.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0088/3790/214f3061-ac62-3502-aec2-3db1d2561bb4.jpg[/img]

以下给出异常产生的实例,红色标记的暂时还没有提供例子。

[b]ArithmeticException[/b]

package my.exception;

public class ArithmeticExceptionTest {

public static void main(String[] args) {
int a = 10;
a = a/0;
}
}


[b]ArrayIndexOutOfBoundsException[/b]

package my.exception;

public class ArrayIndexOutOfBoundsExceptionExample {

public static void main(String[] args) {
int[] array = {1,2,3};
System.out.println(array[3]);
}
}


[b]ArrayStoreException[/b]

package my.exception;

import java.util.HashMap;
import java.util.Map;

public class ArrayStoreExceptionExample {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("数字", 123);
map.put("字符", "Java");

Object[] valueArr = map.values().toArray(new String[map.size()]);
for(int i = 0;i<valueArr.length;i++){
System.out.println(valueArr[i]);
}

}
}


[b]ClassCastException[/b]

package my.exception;

public class ClassCastExceptionExample {
public static void main(String[] args) {
Object x = new Integer(0);
System.out.println((String)x);
}
}


[b]ClassNotFoundException[/b]

package my.exception;

public class ClassNotFoundExceptionExample {

public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("my.exception.ClassNotFoundExceptionExample1");
}
}

[b]CloneNotSupportedException[/b]

package my.exception;

public class CloneNotSupportedExceptionExample {
public static void main(String[] args) throws CloneNotSupportedException {
CloneNotSupportedExceptionExample c2 = (CloneNotSupportedExceptionExample) new CloneNotSupportedExceptionExample()
.clone();
}

}


[b]IllegalArgumentException[/b]

package my.exception;

public class IllegalArgumentExceptionExample {

public static void main(String[] args) {
//Work fine
Season s = Enum.valueOf(Season.class, "SPRING");

//Throws IllegalArgumentException
Season s1 = Enum.valueOf(Season.class, "SPRING111");
}
}

enum Season
{
SPRING,SUMMER,AUTUMN,WINTER;
}


[b]IllegalMonitorStateException[/b]

package my.exception;

public class IllegalMonitorStateExceptionExample {
public static void main(String[] args) throws InterruptedException {
Thread test = new Thread();

test.start();

test.wait();

test.countStackFrames();
}
}


[b]IllegalThreadStateException[/b]

package my.exception;

public class IllegalThreadStateException {

public static void main(String[] args) {
Thread test = new Thread();

//Thread can not be started twice. If we do, then an IllegalThreadStateException occurs.
test.start();

test.start();
}
}


[b]InstantiationException[/b]

package my.exception;

public class InstantiationExceptionExample {
static Object createNewInstance(Object obj) {
try {
return obj.getClass().newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

public static void main(String[] args) {
String[] s = new String[] { "a", "b", "c" };
createNewInstance(s);
}

}


[b]InterruptedException[/b]

package my.exception;

public class InterruptedExceptionExample {

public static void main(String[] args) throws InterruptedException {

final Thread t1 = new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
};
t1.start();
t2.start();

}

}


[b]NegativeArraySizeException[/b]

package my.exception;

public class NegativeArraySizeExceptionExample {

public static void main(String[] args) {
int[] array = new int[-1];
}
}


[b]NoSuchFieldException[/b]

package my.exception;

import java.lang.reflect.Field;

public class NoSuchFieldException {

public static void main(String[] args) throws Exception {

Field field = NoSuchFieldException.class.getField("name");
}

}


[b]NoSuchMethodException[/b]

public class NoSuchMethodExceptionExample {

public static void main(String[] args) {
try {
NoSuchMethodExceptionExample.class.getMethod("getName", String.class);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


[b]NullPointerException[/b]

package my.exception;

public class NullPointerExceptionExample {
public static void main(String[] args) {
String name = null;
System.out.println(name.toLowerCase());
}
}


[b]NumberFormatException[/b]

package my.exception;

public class NumberFormatExceptionExample {

public static void main(String[] args) {
int value = Integer.valueOf("123.25f");
}
}


[b]StringIndexOutOfBoundsException[/b]

package my.exception;

public class StringIndexOutOfBoundsExceptionExample {

public static void main(String[] args) {
String value = "hello world!";
char c = value.charAt(20);
}
}


[b]UnsupportedOperationException[/b]

package my.exception;

import java.util.Arrays;
import java.util.List;

public class UnsupportedOperationExceptionExample {
public static void main(String[] args) {
String[] array = {"Hello","World","Java"};
List<String> test = Arrays.asList(array);
//Arrays.asList获取的list是不能用于删除的
test.remove("Hello");

}
}


对于上述标红的,暂时没有例子的异常,博友们有直接产生的例子也请共享哦。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值