JAVA实验报告九异常处理_Java课后练习9(异常处理)
動(dòng)手動(dòng)腦1:
import javax.swing.*;
class AboutException {
public static void main(String[] a)
{
int i=1, j=0, k;
k=i/j;
try
{
k = i/j; // Causes division-by-zero exception
//throw new Exception("Hello.Exception!");
}
catch ( ArithmeticException e)
{
System.out.println("被0除. "+ e.getMessage());
}
catch (Exception e)
{
if (e instanceof ArithmeticException)
System.out.println("被0除");
else
{
System.out.println(e.getMessage());
}
}
finally
{
JOptionPane.showConfirmDialog(null,"OK");
}
}
}
輸出結(jié)果:
動(dòng)手動(dòng)腦2:
代碼:
public class CatchWho {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/內(nèi)層try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("發(fā)生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch");
}
}
輸出結(jié)果:
動(dòng)手動(dòng)腦3;
源代碼:
public class EmbededFinally {
public static void main(String args[]) {
int result;
try {
System.out.println("in Level 1");
try {
System.out.println("in Level 2");
// result=100/0; //Level 2
try {
System.out.println("in Level 3");
result=100/0; //Level 3
}
catch (Exception e) {
System.out.println("Level 3:" + e.getClass().toString());
}
finally {
System.out.println("In Level 3 finally");
}
// result=100/0; //Level 2
}
catch (Exception e) {
System.out.println("Level 2:" + e.getClass().toString());
}
finally {
System.out.println("In Level 2 finally");
}
// result = 100 / 0; //level 1
}
catch (Exception e) {
System.out.println("Level 1:" + e.getClass().toString());
}
finally {
. System.out.println("In Level 1 finally");
}
}
}
輸出結(jié)果:
動(dòng)手動(dòng)腦4:當(dāng)有多層嵌套的finally時(shí),異常在不同的層次拋出 ,在不同的位置拋出,可能會(huì)導(dǎo)致不同的finally語句塊執(zhí)行順序。
源代碼:
public class SystemExitAndFinally {
public static void main(String[] args)
{
try{
System.out.println("in main");
throw new Exception("Exception is thrown in main");
//System.exit(0);
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
finally
{
System.out.println("in finally");
}
}
}
輸出結(jié)果:
動(dòng)手動(dòng)腦5:
finally語句塊一定會(huì)執(zhí)行嗎?
至少有兩種情況下finally語句是不會(huì)被執(zhí)行的:
(1)try語句沒有被執(zhí)行到,如在try語句之前return就返回了,這樣finally語句就不會(huì)執(zhí)行。這也說明了finally語句被執(zhí)行的必要而非充分條件是:相應(yīng)的try語句一定被執(zhí)行到。
(2)在try塊|catch塊中有System.exit(0);這樣的語句。System.exit(0)是終止java虛擬機(jī)JVM的,連JVM都停止了,所有都結(jié)束了,當(dāng)然finally語句也不會(huì)被執(zhí)行到。
源代碼:
// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {
public static void main( String args[] )
{
try {
method1();
}
catch ( Exception e ) {
System.err.println( e.getMessage() + "\n" );
e.printStackTrace();
}
}
public static void method1() throws Exception
{
method2();
}
public static void method2() throws Exception
{
method3();
}
public static void method3() throws Exception
{
throw new Exception( "Exception thrown in method3" );
}
}
輸出截圖:
動(dòng)手動(dòng)腦6:
如何跟蹤異常的傳播路徑?
當(dāng)程序中出現(xiàn)異常時(shí),JVM會(huì)依據(jù)方法調(diào)用順序依次查找有關(guān)的錯(cuò)誤處理程序。
可使用printStackTrace 和 getMessage方法了解異常發(fā)生的情況: printStackTrace:打印方法調(diào)用堆棧。
每個(gè)Throwable類的對(duì)象都有一個(gè)getMessage方法,它返回一個(gè)字串,這個(gè)字串是在Exception構(gòu)造函數(shù)中傳入的,通常讓這一字串包含特定異常的相關(guān)信息。
源代碼:
import java.io.*;
public class ThrowMultiExceptionsDemo {
public static void main(String[] args)
{
try {
throwsTest();
}
catch(IOException e) {
System.out.println("捕捉異常");
}
}
private static void throwsTest() throws ArithmeticException,IOException {
System.out.println("這只是一個(gè)測(cè)試");
// 程序處理過程假設(shè)發(fā)生異常
throw new IOException();
//throw new ArithmeticException();
}
}
輸出截圖:
一個(gè)方法可以聲明拋出多個(gè)異常
int g(float h) throws OneException,TwoException { …… }
這只是一個(gè)測(cè)試
捕捉異常
import java.io.*;
public class Test
{
public void test()throws IOException
{
FileInputStream fis = new FileInputStream("a.txt");
}
}
class Sub extends Test
{
//如果test方法聲明拋出了比父類方法更大的異常,比如Exception
//則代碼將無法編譯……
public void test() throws FileNotFoundException
{
//...
}
}
一個(gè)子類的throws子句拋出的異常,不能是其基類同名方法拋出的異常對(duì)象的父類。
動(dòng)手動(dòng)腦7:
編寫一個(gè)程序,此程序在運(yùn)行時(shí)要求用戶輸入一個(gè) 整數(shù),代表某門課的考試成績(jī),程序接著給出“不及格”、“及格”、“中”、“良”、“優(yōu)”的結(jié)論。
要求程序必須具備足夠的健壯性,不管用戶輸入什 么樣的內(nèi)容,都不會(huì)崩潰。
源代碼:
import javax.swing.JOptionPane;
public class Score {
public static void main(String[] args){
for(;;)
{
String a = JOptionPane.showInputDialog("請(qǐng)輸入一個(gè)成績(jī):");
try{
int b = Integer.parseInt(a);
if(b>0&&b<60){
JOptionPane.showMessageDialog(null,
"不及格!");
break;
}
else if(b>=60&&b<=70)
{
JOptionPane.showMessageDialog(null,
"及格!");
break;
}
else if(b>70&&b<=80)
{
JOptionPane.showMessageDialog(null,
"成績(jī)中等!");
break;
}
else if(b>80&&b<=90)
{
JOptionPane.showMessageDialog(null,
"成績(jī)良好!");
break;
}
else if(b>90&&b<=100)
{
JOptionPane.showMessageDialog(null,
"成績(jī)優(yōu)秀!");
break;
}
else if(b>100||b<0)
{
JOptionPane.showMessageDialog(null,
"您輸入的成績(jī)超出范圍,請(qǐng)重新輸入!");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,
"您的輸入有誤,請(qǐng)重新輸入!");
}
}
}
}
輸出截圖:
總結(jié)
以上是生活随笔為你收集整理的JAVA实验报告九异常处理_Java课后练习9(异常处理)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 配置参数_给你的JAVA程序配
- 下一篇: Java 重写 多态性_java多态性重