Java “Unhandled exception type Exception”错误提示 (转)
2012-03-09 01:46
351 查看
原文地址: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();
}
}
}
在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();
}
}
}
相关文章推荐
- JAVA错误:使用rs.next()提示"Unhandled exception type SQLException“
- Java “Unhandled exception type Exception”错误提示
- Java “Unhandled exception type Exception”错误提示 (转)
- java读取数据库中的错误:"Unhandled exception type SQLException"
- Java错误提示-------java.net.SocketException:Unrecognized Windows Sockets error: 0: JVM_Bind
- 大数据开发中遇到的错误 java.lang.RuntimeException: java.io.IOException: invalid constant type: 18
- 另人抓狂的错误:java.lang.IllegalArgumentException: No property find found for type class
- 【Oracle-JSP-Eclipse】已经引入jar包并且build path了,还是提示java.lang.ClassNotFoundException之类的错误
- java错误提示:Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.gener
- 关于java.lang.reflect.MalformedParameterizedTypeException错误的问题
- 启动eclipse时候提示错误Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred
- Cassandra 2.x 提示“错误: 代理抛出异常错误: java.lang.NullPointerException”
- JAVA 新手问题: Request 编码编译出错,Unhandled exception type UnsupportedEncodingException
- php has encountered an unhandled exception code 错误提示的解决方法
- C# 读取app.config配置文件 节点键值,提示 "System.TypeInitializationException 类型初始值设定项引发异常" 错误的解决方案
- java错误提示------ The public type must be defined in its own file
- 错误提示:Caused by: org.hibernate.MappingException: Could not determine type for
- C#初始化数据库错误提示:The type initializer for 'ASSET.DBHelper' threw an exception
- java中,遇到“Unhandled exception type……”怎么处理?
- java.lang.IllegalArgumentException: argument type mismatch 错误