by Dinesh Thakur Category: Exception Handling

The basic principle of Java error handling mechanism is that an exception must either be handled by the method in which it is raised or passed along the call chain for another method to handle it. Suppose a method throws an exception, that is neither a subclass of RuntimeException nor of Error i.e. if it throws a checked exception, then Java requires that the method either handles it or declares it. If the method does not handle the checked exception then the method must declare it using the throws keyword.

The following example demonstrates the use of throws clause


public class ThrowsClause
     static boolean guess() throws IOException
         char ch='r';
         System.out.print("Guess any Character(a-z) : ");
         char n=(char);
     public static void main(String[] args)
          boolean result;
                 System.out.println("Your Guess is Perfect");
                 System.out.println("Your Guess is Incorrect");
              catch(IOException e)
                 System.out.println("Error May be Occured in Input");

Throws Clause in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content