Is it mandatory to handle an exception that is thrown using the throw keyword? If so is there a separate rule for Checked and Unchecked Exception, as in who should be mandatorily caught?

 public void met() {
         throw new ArithmeticException

there is no rule as to "who should catch it" that's up to you. you can throw it indefinitely, even make the main method throw it, but that's just a system crash waiting to happen.
it's actually the difference between a 'checked' and 'unchecked'. a Checked exception must be either thrown or handled with. an Unchecked exception .. well, not really. the compiler doesn't force you to, doesn't mean that taking into account something can go wrong is such a bad idea, though :)

let's just state this scenario:
you want to get a file from your disk, based on an integer you pass to the method. if it is 0, an ArithmeticException should be thrown (yes, I know, not really what it is meant for, just an example).
if it's not 0, the method should create an instance of File with parameter: ("File"+theIntYouPassed+".txt") and, if it doesn't exist, throw an FileNotFoundException.

now, we have both a Checked (FileNotFoundException) and an UnChecked (ArithemticException) in one method:

public static File getFile(int d){
    if ( d == 0 )
        throw new ArithmeticException("ArithmeticExceptionThrown");
    File f = new File("File"+d+".txt");
    if ( !f.exists())
        throw new FileNotFoundException("file not found");
    else
        return f;       
}

if you try to compile the above, the compiler will refuse. reason: you have a Checked exception which isn't caught (try-catch block) nor thrown by the method (throws clause). any of the below suggestions would fix this:

  • throws

    public static File getFile(int d)
    throws FileNotFoundException{
    if ( d == 0 )
    throw new ArithmeticException("ArithmeticExceptionThrown");
    File f = new File("File"+d+".txt");
    if ( !f.exists())
    throw new FileNotFoundException("file not found");
    else
    return f;
    }

now, if you want to solve it like this, the method calling this method should either:
contain a try-catch block, catching the FileNotFoundException, or it should add a throws clause for itself, because it is possible a FileNotFoundException will be thrown to it.

  • catching it

    public static File getFile(int d){
    if ( d == 0 )
    throw new ArithmeticException("ArithmeticExceptionThrown");
    File f = new File("File"+d+".txt");
    if ( !f.exists())
    try{
    throw new FileNotFoundException("file not found");
    }
    catch(FileNotFoundException fnfe){
    fnfe.printStackTrace();
    return null;
    }
    else
    return f;
    }

here, I am catching and handling the Exception locally. since it will never reach the method that calls getFile, that method won't have to deal with the exception.

commented: thanks a ton stultuske, that was a very detailed and concise example :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.