Java “Unhandled exception type Exception”错误提示 (转)
来源:互联网 发布:多迪网络是培训机构嘛 编辑:程序博客网 时间:2024/05/16 16:00
原文地址:http://www.olnote.com/itlife/note/100000089.html
在Java中除了RuntimeException及其任何子类,其他异常类都被Java的异常强制处理机制强制异常处理。
关于那些被强制异常处理的代码块,必须进行异常处理,否则编译器会提示“Unhandled exception type Exception”错误警告。
- Java中用于处理异常的方式
- 自行处理:可能引发异常的语句封入在try内,而处理异常的相应语句则封入catch块内
回避异常:在方法声明中包含throws子句,通知潜在调用者,如果发生了异常,必须由调用者处理。 - 示例
- package Test;
import java.io.*;
import javax.swing.JOptionPane;
public class TestExceptionHandle {
/**
* @param args
*/
public static void main(String[] args) {
// JOptionPane.showInputDialog可能会抛出HeadlessException,HeadlessException是RuntimeException的子类,编译器并不会强制异常处理。
String fileName = JOptionPane.showInputDialog("FileName=");
// 通过try...catch语句自行处理异常。
try {
String str = ReadFile(fileName);
JOptionPane.showMessageDialog(null, str);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getStackTrace());
}
}
/**
* 读取文件内容并以文本形式返回。
*
* @param fileName
* 文件名。
* @return 文件内容。
* @throws IOException
* -当IO错误时发生。
*/
// 用throws声明该方法可能抛出的异常信息,从而回避异常。
public static String ReadFile(String fileName) throws IOException {
FileReader fleReader = null;
try {
fleReader = new FileReader(fileName);
StringBuffer buffer = new StringBuffer();
char data[] = new char[1024];
int count = 0;
do {
count = fleReader.read(data);
if (count != -1) {
buffer.append(data, 0, count);
}
} while (count != -1);
return buffer.toString();
} finally {
fleReader.close();
}
}
}
0 0
- Java “Unhandled exception type Exception”错误提示 (转)
- java读取数据库中的错误:"Unhandled exception type SQLException"
- unhandled exception type UnsupportedEncodingException
- Unhandled exception type ParseException
- unhandled exception 错误
- Unhandled exception type UsersException解决方法!
- Unhandled exception type SQLException 异常
- java中,遇到“Unhandled exception type……”怎么处理?
- java中,遇到“Unhandled exception type……”怎么处理?
- java中,遇到“Unhandled exception type……”怎么处理?
- unhandled exception
- An unhandled exception of type 'System.StackOverflowException'
- 使用rs.next()提示"Unhandled exception type SQLException"两种解决方法
- 错误:Unhandled Exception: System.TypeLoadException:Could not load type 'Newtonsoft.Json.Linq.JContaine
- Unhandled Exception thrown: class java.lang.NullPointerException
- Unhandled event loop exception Java heap space
- NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
- Unhandled event loop exception Java heap space
- poj 2828POJ2828 Buy Tickets【线段树,逆序遍历】
- 51nod dai1163 最高的奖励
- IT学习网址
- ueditor1_4_3-utf8-jsp版使用配置
- xshell-常用命令
- Java “Unhandled exception type Exception”错误提示 (转)
- cmake 教程
- PyCharm2016.3版首次安装说明
- 《编程珠玑》习题练习In Python——第二章 啊哈!算法
- js金额格式化
- rem 失效
- java基础部分总结
- 欢迎使用CSDN-markdown编辑器
- 霍夫曼编码