Java中getMessage()和printStackTrace方法

public class ExceptionTest07{

	public static void main(String[] args){

		try{

			FileInputStream fis = new FileInputStream("c:/abc.txt");

			//JVM为我们执行了一下这段代码
			//FileNotFoundException e = new FileNotFoundException("c:\abc.txt (系统找不到指定的文件。)");

		}catch(FileNotFoundException e){

			//打印异常堆栈信息.
			//一般情况下都会使用该方式去调试程序.
			//e.printStackTrace();

			/*
			java.io.FileNotFoundException: c:\abc.txt (系统找不到指定的文件。)
		        at java.io.FileInputStream.open(Native Method)
		        at java.io.FileInputStream.<init>(FileInputStream.java:106)
		        at java.io.FileInputStream.<init>(FileInputStream.java:66)
		        at ExceptionTest07.main(ExceptionTest07.java:13)
        */
        
        String msg = e.getMessage();//e.getMassage的来源于JVM为我们执行了一下这段代码,FileNotFoundException e = new FileNotFoundException("c:\abc.txt (系统找不到指定的文件。)");
        
        System.out.println(msg); //c:\abc.txt (系统找不到指定的文件。)
        

		}

		//这段代码会执行.
		System.out.println("ABC");

	}
}
时间: 2024-08-02 17:36:53

Java中getMessage()和printStackTrace方法的相关文章

java中的jdbc连接数据库方法及应用

jdbc连接数据库的口诀:猪脸特直观 import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Demo03 { public static void main(String[] args) thr

Java中vector的使用方法

Vector的使用 vector类底层数组结构的,它包含可以使用整数索引进行访问的组件.不过,vector的大小可以根据需要增大或缩小,以适应创建vector后进行添加或移除项的操作,因此不需要考虑元素是否越界或者会不会浪费内存的问题. 由vector的iterator和listIterator方法所返回的迭代器是快速失败的:也即是它不能并发执行操作.如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代器自身的remove或add方法之外的任何其他方式),则迭代器将抛出ConcurrentM

Java中Integer类的方法

字段摘要 static int MAX_VALUE              保持 int 类型的最大值的常量可取的值为 231-1. static int MIN_VALUE              保持 int 类型的最小值的常量可取的值为 -231. static int SIZE              以二进制补码形式表示 int 值的位数. static Class<Integer> TYPE              表示基本类型 int 的 Class 实例. 构造方法摘要

java中substring的使用方法

str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex開始至endIndex结束时的字符串,并将其赋值给str; 下面是一段演示程序: public class StringDemo{ public static void main(String agrs[]){ Str

转- 关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)

package cn.outofmemory.codes.Date; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); System.out.println("现在时间

Java中循环声明变量方法

Java循环声明变量 之前想这样做,但是网上一直搜索不到,下面是我的方式 项目中 // 得到需要查询外表的数量,然后分别创建缓存,插入数据多的时候如果编码在缓存里面,就不需要再去查询数据库了.key:code/value:pk // 根据"数据来源"有多少非空的 就创建几个,使用 "数据来源字段"+Cache 当cacheMap的key Map<String, Map<String, String>> cacheMap =new HashMa

关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)(一)

package cn.outofmemory.codes.Date; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); System.out.println("现在时间

java中字符串切割的方法总结

StringTokenizer最快 ,基本已经不用了,除非在某些需要效率的场合.Scanner最慢. String和Pattern速度差不多.Pattern稍快些. String和Pattern的split 方法效率相当,常用 public   static   void  main(String [] args){ long  start = System.currentTimeMillis(); for ( int  i= 0 ;i< 100000 ;i++){ test1(); } lon

Java中StringBuffer类append方法的使用

Java中StringBuffer类append方法的使用 append方法的作用是在一个StringBuffer对象后面追加字符串. 例如StringBuffer s = new StringBuffer("Hello");s.append("World");则s的内容是HelloWorld