The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Reserved word "throws"

Heyey everyone. I've read many articles about handling exception but some do not discuss throws briefly and others do not discuss em at all.

I get the idea of try/catch blocks and somewhat of the reserved word "finally."
What i don't get is why one does not need the catch block when one adds throws Specific Exception or Exception in the main method. What happens when the specified exception occur? Explicitly, what does the reserved word throws do?

Re: Reserved word "throws"

Throw means that at that point, an object (almost always an Exception) gets thrown, and the program skips over all other code until that object is caught.

There are two types of Exceptions: checked and unchecked. Basically, the difference is the compiler forces you to catch checked exceptions and doesn't for unchecked exceptions. If a method is declared that it throws a certain type of checked Exception (it can be declared to throw unchecked exceptions, too but it doesn't force anyone to catch them), it simply means that the method doesn't have to catch that Exception if it's thrown somewhere inside, but who-ever is calling that method must catch it (unless that method is declared to throw that same type of exception, too).

If you declare your main method to throw Exception, that means it doesn't have to catch it. The JVM will automatically catch all exceptions that come back to it, i.e. any that weren't caught and have percolated up past the main method.