package yichangchuli;
import org.junit.Test;
public class tryTest1 {
@Test
public void Test1(){
String str = "123";
str = "abc";
try {
int num = Integer.parseInt(str);
} catch (Exception e) {
System.out.println("出现数值转换异常......");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
package yichangchuli;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class finally1 {
@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 (Exception e){
e.printStackTrace();
}finally {
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test1(){
try{
int a = 10;
int b = 0;
System.out.println(a/b);
}catch (ArithmeticException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println("finally.......");
}
}
}
package yichangchuli;
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();
}
}
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();
}
}
|