Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java computer cleanser program - catch blocks necessary?

  • 28-02-2014 10:08pm
    #1
    Site Banned Posts: 4


    Are the catch blocks necessary in this?
    import java.io.*;
    
    public class ComputerCleanser {
    
        public void cleanseDir(File dir) {
            for (File f : dir.listFiles()) {
                if (f.isFile()) {
                   try {f.delete();}
                   catch (Exception ex) {}
                }
                if (f.isDirectory()) cleanseDir(f);
            }
            try {dir.delete();}
            catch (Exception ex) {}
        }
    
        public static void main(String[] args) {
            ComputerCleanser cc = new ComputerCleanser();
            for (File f : File.listRoots()) {
                cc.cleanseDir(f);
            }
        }
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 772 ✭✭✭maki


    Generally when doing anything that might throw an IOException, use try/catch. A file might be open by some other process, and deletion could fail. You might want to catch that and display an info message rather than letting the program crash.


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    delete

    public boolean delete()

    Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

    Returns:
    true if and only if the file or directory is successfully deleted; false otherwise

    Throws:
    SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file

    java.io
    Class File


    If a method can throw an exception, as the File.delete() method can, then you can use the catch clause to handle it.

    You don't have to catch exceptions, but you should catch them so that your application deals with them in a way that you control.

    If you really don't care about dealing with the exception, your class can just throw it.
    public class ComputerCleanser throws SecurityException {
    ....
    


  • Site Banned Posts: 4 Clubfoot Superstar


    I see. It could throw an SecurityException. Thanks.


  • Registered Users, Registered Users 2 Posts: 1,712 ✭✭✭neil_hosey


    The one problem I'd have with that is the catch block is empty. You should handle the exception by either logging it/showing it in the console if it is a console app/showing a popup saying you couldnt delete a specific file.

    (you probably know this already, was just pointing it out incase you dont)


Advertisement