We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 17: More Exceptions
  5. Tutorial

Day 17: More Exceptions

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial


Terms you'll find helpful in completing today's challenge are outlined below, along with sample Java code (where appropriate).

Propagating Exceptions
If an exception is not caught by the method that threw it, the program's control is transferred back (propagated) to the calling method (i.e.: whatever called the method that threw the exception). This can be good if you have designed your program to handle exceptions at a high level, but bad if you never write code to catch the exception in the calling methods that the exception is being propagated to. The example method below throws an exception of type Exception, which propagates back to the calling method (main), where a catch block catches it and prints a message:

class PropagatedException {

    void example() throws Exception{
        throw new Exception("This exception will always be thrown.");
    }
    
    public static void main(String[] args) {
        PropagatedException p = new PropagatedException();
        try{
            p.example();
        }
        catch(Exception e){
            System.err.println( e.getClass().getSimpleName() + ": " + e.getMessage() );
        }
    }
}

The above code throws a java.lang.Exception and prints:

Exception: This exception will always be thrown.


Confused? Check out yesterday's introduction to Exceptions.


Solve Problem

tutorial details


Tutorial By

AllisonP

Video By

blondiebytes

Solve Problem

Need Help?


View discussions
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy