6-1 jmu-Java-06 exception-finally

In JAVA.水.4
code, apply for resources from the system, and release the resources at the end.
There is a Resource class representing resource classes, including methods:

open(String str) opens the resource and declares to throw Exception (including error information).
The close() method releases resources and declares to throw RuntimeException (including error information)

Now according to the difference of str in open(String str), different information is printed. The content of str is divided into 4 cases:

fail fail, which means that both open and close will be abnormal. Print open error information and close error information.
Fail success means that open throws an exception and prints open error information. Close executes normally, print resource release success
success fail, which means open executes normally, print resource open success. Close throws an exception and prints close error information.
Success success means open is executed normally, resource open success is printed, and close is executed normally, resource release success is printed.

Note 1: You do not need to write code to print error messages.
Note 2: After catching the exception, use System.out.println(e) to output the exception information, e is the exception generated.
Referee test procedure:

public static void main(String[] args) {
    
    
Scanner sc = new Scanner(System.in);
    Resource resource = null;
try{
    
    
        resource = new Resource();
        resource.open(sc.nextLine());
   /*这里放置你的答案*/
sc.close();
}

The following input sample represents the input of success success.

Input sample

success success

Sample output

resource open success
resource release success

AC code:

 	System.out.println("resource open success");
}
catch(Exception e)
{
    
     
   	System.out.println(e);
}
try{
    
    
    	resource.close();
    	System.out.println("resource release success");
}
catch(RuntimeException e)
{
    
    
 	System.out.println(e);
}

Guess you like

Origin blog.csdn.net/weixin_45989486/article/details/109336680