Java “Unhandled exception type Exception”错误提示 (转)
2016-12-02 19:39
931 查看
原文地址: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 “Unhandled exception type Exception”错误提示
- JAVA错误:使用rs.next()提示"Unhandled exception type SQLException“
- Java “Unhandled exception type Exception”错误提示 (转)
- java读取数据库中的错误:"Unhandled exception type SQLException"
- 错误提示:java.lang.NoSuchMethodException
- Cassandra 2.x 提示“错误: 代理抛出异常错误: java.lang.NullPointerException”
- 使用rs.next()提示"Unhandled exception type SQLException"两种解决方法
- 错误提示:java.lang.IllegalArgumentException: node to traverse cannot be null!
- Rational Rose 启动和退出的错误,提示java.lang.ClassNotFoundException
- java错误提示------ The public type must be defined in its own file
- oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null” 错误,导致EM无法使用
- java中,遇到“Unhandled exception type……”怎么处理?
- php has encountered an unhandled exception code 错误提示的解决方法
- Tomcat启动报Error listenerStart错误 | "beans" 必须匹配 DOCTYPE 根 "null" | java.lang.reflect.MalformedParameterizedTypeException
- Java错误提示--------Only a type can be imported. java.awt.event resolves to a package
- Java:简单的设置自己的Exception错误提示
- weblogic出现Caused by: java.io.StreamCorruptedException: invalid type code: 31错误
- java.lang.IllegalArgumentException: argument type mismatch 错误
- 另人抓狂的错误:java.lang.IllegalArgumentException: No property find found for type class
- RationalRose启动提示java.lang.class.not found Exception错误的解决方法