例子1
package com.atguigu.java;
public class ErrorTest {
public static void main(String[] args) {
Integer[] arr = new Integer[1024*1024*1024];
}
}
例子2
package com.atguigu.java;
import java.sql.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
@Test
public void test7(){
int a = 10;
int b = 0;
System.out.println(a/b);
}
@Test
public void test6(){
Scanner scanner =new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
}
@Test
public void test5(){
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
@Test
public void test4(){
Object obj = new Date(0);
String str = (String) obj;
}
@Test
public void test3(){
String str = "abc";
System.out.println(str.charAt(3));
}
@Test
public void test2(){
int[] arr = new int[10];
System.out.println(arr[10]);
}
@Test
public void test1(){
int[] arr = null;
System.out.println(arr[3]);
String str = "abc";
str=null;
System.out.println(str.charAt(0));
}
}
例子3
package com.atguigu.java;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test1(){
String str = "123";
str = "abc";
try{
int num = Integer.parseInt(str);
}catch(NumberFormatException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
例子4
package com.atguigu.java;
import org.junit.Test;
public class FinallyTest {
@Test
public void testMethod(){
int num = method();
System.out.println(num);
}
public int method(){
try{
int[] arr = new int[10];
System.out.println(arr[10]);
return 1;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
return 2;
}finally{
System.out.println("我一定会被执行");
return 3;
}
}
@Test
public void test1(){
try{
int a=10;
int b=0;
System.out.println(a/b);
}catch(ArithmeticException e){
int[] arr = new int[10];
System.out.println(arr[10]);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("我好帅啊~~");
}
}
}
例子5
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2(){
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
int data= fis.read();
while(data!=-1){
System.out.println((char)data);
data = fis.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例子6
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{
method2();
}catch(IOException e){
e.printStackTrace();
}
method3();
}
public static void method3(){
try{
method2();
}catch (IOException e){
e.printStackTrace();
}
}
public static void method2() throws IOException{
method1();
}
public static void method1() throws FileNotFoundException,IOException{
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
int data= fis.read();
while(data!=-1){
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
}
例子7
package com.atguigu.java;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest test = new OverrideTest();
test.display(new SubClass());
}
public void display(SuperClass s){
try{
s.method();
}catch (IOException e){
e.printStackTrace();
}
}
}
class SuperClass{
public void method() throws IOException{
}
}
class SubClass extends SuperClass{
public void method() throws FileNotFoundException{
}
}
例子8
package com.atguigu.java2;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-10);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Student{
int id;
public void regist(int id) throws Exception{
if(id>0){
this.id=id;
}else{
throw new Exception("您输入的数据非法!");
}
}
public String toString() {
return "Student [id=" + id + "]";
}
}
例子9
package com.atguigu.java2;
public class MyException extends Exception{
static final long serialVersionUID = -7034897190745766939L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
package com.atguigu.java2;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-10);
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Student{
int id;
public void regist(int id) throws Exception{
if(id>0){
this.id=id;
}else{
throw new MyException("不能输入负数");
}
}
public String toString() {
return "Student [id=" + id + "]";
}
}