System.in.read() IOException RuntimeException
来源:互联网 发布:淘宝商城加盟代理 编辑:程序博客网 时间:2024/05/01 19:57
throws 关键字在方法声明中使用,用来列出任何可由方法引发的、不是从 Error 或 RuntimeException 派生的异常。
备注
若要避免程序异常结束,要么在可能引发异常的方法的声明中使用 throws,要么使用 try-catch 或 try-catch-finally 语句处理异常。
若要了解某个特定方法会引发哪种异常,可在不使用 throws 子句的情况下编译程序,编译器会发出错误消息,告诉您应声明或捕捉的异常的名称。
示例 1
此示例使用 System.in.read 方法从键盘读取一个字符。此方法必须与 throws 子句一起使用;否则,会引发异常 java.io.IOException。如果在不使用 throws 子句的情况下运行此示例,则会出现编译时错误消息“Exception 'java.io.IOException' is not caught and does not appear in throws clause”。
// Throws1.jsl
// throws example
import java.io.*;
class MyClass
{
// Use the throws keyword with the method that is expected to throw
// an exception:
public static void main(String[] args) throws IOException
{
System.out.print("Please enter a character: ");
// Read a character from the keyboard:
char c = (char)System.in.read();
// Display the character:
System.out.println("You entered the charcater " + c);
}
}
演示如何在 try-catch 语句内使用 System.in.read 来避免程序异常结束。catch 块用于捕捉异常 java.io.IOException。不过,这种情况下可以使用超类 java.lang.Exception。
// Throws2.jsl
// throws example
import java.io.*;
class MyClass
{
public static void main(String[] args)
{
// Put the code that may throw an exception inside a try-catch block:
try
{
System.out.print("Please enter a character: ");
// Read a character from the keyboard:
char c = (char)System.in.read();
// Display the character:
System.out.println("You entered the character " + c);
}
// The catch block may handle the exception or may be empty:
catch (IOException e)
{
System.out.print("Exception caught!");
}
}
}
异常的分类:
① 异常的继承结构:基类为Throwable,Error和Exception继承Throwable,RuntimeException和IOException等继承Exception,具体的RuntimeException继承RuntimeException。
② Error和RuntimeException及其子类成为未检查异常(unchecked),其它异常成为已检查异常(checked)。
每个类型的异常的特点
Error体系 : Error类体系描述了Java运行系统中的内部错误以及资源耗尽的情形。应用程序不应该抛出这种类型的对象(一般是由虚拟机抛出)。如果出现这种错误,除了尽力使程序安全退出外,在其他方面是无能为力的。所以,在进行程序设计时,应该更关注Exception体系。
Exception体系:包括RuntimeException体系和其他非RuntimeException的体系 :
① RuntimeException:RuntimeException体系包括错误的类型转换、数组越界访问和试图访问空指针等等。处理RuntimeException的原则是:如果出现RuntimeException,那么一定是程序员的错误。例如,可以通过检查数组下标和数组边界来避免数组越界访问异常。 ②其他非RuntimeException(IOException等等):这类异常一般是外部错误,例如试图从文件尾后读取数据等,这并不是程序本身的错误,而是在应用环境中出现的外部错误。
备注
若要避免程序异常结束,要么在可能引发异常的方法的声明中使用 throws,要么使用 try-catch 或 try-catch-finally 语句处理异常。
若要了解某个特定方法会引发哪种异常,可在不使用 throws 子句的情况下编译程序,编译器会发出错误消息,告诉您应声明或捕捉的异常的名称。
示例 1
此示例使用 System.in.read 方法从键盘读取一个字符。此方法必须与 throws 子句一起使用;否则,会引发异常 java.io.IOException。如果在不使用 throws 子句的情况下运行此示例,则会出现编译时错误消息“Exception 'java.io.IOException' is not caught and does not appear in throws clause”。
// Throws1.jsl
// throws example
import java.io.*;
class MyClass
{
// Use the throws keyword with the method that is expected to throw
// an exception:
public static void main(String[] args) throws IOException
{
System.out.print("Please enter a character: ");
// Read a character from the keyboard:
char c = (char)System.in.read();
// Display the character:
System.out.println("You entered the charcater " + c);
}
}
演示如何在 try-catch 语句内使用 System.in.read 来避免程序异常结束。catch 块用于捕捉异常 java.io.IOException。不过,这种情况下可以使用超类 java.lang.Exception。
// Throws2.jsl
// throws example
import java.io.*;
class MyClass
{
public static void main(String[] args)
{
// Put the code that may throw an exception inside a try-catch block:
try
{
System.out.print("Please enter a character: ");
// Read a character from the keyboard:
char c = (char)System.in.read();
// Display the character:
System.out.println("You entered the character " + c);
}
// The catch block may handle the exception or may be empty:
catch (IOException e)
{
System.out.print("Exception caught!");
}
}
}
异常的分类:
① 异常的继承结构:基类为Throwable,Error和Exception继承Throwable,RuntimeException和IOException等继承Exception,具体的RuntimeException继承RuntimeException。
② Error和RuntimeException及其子类成为未检查异常(unchecked),其它异常成为已检查异常(checked)。
每个类型的异常的特点
Error体系 : Error类体系描述了Java运行系统中的内部错误以及资源耗尽的情形。应用程序不应该抛出这种类型的对象(一般是由虚拟机抛出)。如果出现这种错误,除了尽力使程序安全退出外,在其他方面是无能为力的。所以,在进行程序设计时,应该更关注Exception体系。
Exception体系:包括RuntimeException体系和其他非RuntimeException的体系 :
① RuntimeException:RuntimeException体系包括错误的类型转换、数组越界访问和试图访问空指针等等。处理RuntimeException的原则是:如果出现RuntimeException,那么一定是程序员的错误。例如,可以通过检查数组下标和数组边界来避免数组越界访问异常。 ②其他非RuntimeException(IOException等等):这类异常一般是外部错误,例如试图从文件尾后读取数据等,这并不是程序本身的错误,而是在应用环境中出现的外部错误。
0 0
- System.in.read() IOException RuntimeException
- Android Read Only File System IOException
- 关于System.in.read()
- 关于System.in.read()
- System.in.read()问题
- System.in.read()方法
- System.in.read()使用
- System.in.read()用法
- system.in.read()用法
- system.in.read()用法
- System.in.read()使用
- 【Android】报错IOException: Read-only file system问题解决方法
- System.in.read()的特点
- System.in.read()的用法
- System.in.read()的用法
- System.in中的read()方法
- java输入system.in.read()
- System.in.read()的使用方法
- 【算法练习】
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- 【日志】2014_04_01 ssh 的小结
- 浅谈REDIS数据库的键值设计
- GUN automake,autoconf写Makefile(一)
- System.in.read() IOException RuntimeException
- dmosaic资料
- 堆和栈的区别
- java 反射
- JS正则表达式获取字符串中得特定字符
- 随笔吐槽
- win7(win8)下硬盘安装redhat企业版v6双系统
- 简单的通讯薄
- 最长公共子序列