We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,599 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Throwing an error and handling it.

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
2
Contributors
1
Reply
28 Minutes
Discussion Span
8 Months Ago
Last Updated
2
Views
Question
Answered
rahul.ch
Junior Poster in Training
90 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

stultuske
Industrious Poster
4,493 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 630
Skill Endorsements: 25
Question Answered as of 8 Months Ago by stultuske

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0577 seconds using 2.73MB