File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Throwing Exceptions from a Java Newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Throwing Exceptions from a Java Newbie" Watch "Question about Throwing Exceptions from a Java Newbie" New topic
Author

Question about Throwing Exceptions from a Java Newbie

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Hi everyone, I got a question on a topic that has always bothered me. EXCEPTIONS.
I know:
1. the rule is handle(try/catch) or declare (throws)
2. There are 3 types of Errors (Checked Exceptions, Runtime Exceptions, and Error).
3. The only type of Error that can be handled are Checked exceptions.

Here are my questions:
1--If an exception isn't handled or declared properly does it cause compilation errors OR runtime errors?
2--Does the code still compile?
I thought checked exceptions were stopped by the compiler which prevented compilation, and runtime exceptions & errors would allow the code to compile, but not run. But I was talking to someone the other day about this topic and I was left confused.

3--What's the difference between use of the keywords "throws" & "throw"? I always think "throws" is actually throwing the exception.

4--Any good tips for nailing down Exceptions? The questions in the K&B SCJP6 book are pretty tricky (I can only imagine what the exam is going to be like).

Thanks in advance.


SCJP 6.0
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
3. The only type of Error that can be handled are Checked exceptions.


You can handle or declare any of the three, but you MUST handle or declare checked exceptions.

1--If an exception isn't handled or declared properly does it cause compilation errors OR runtime errors?


You get a compiler error if your code doesn't handle or declare a checked exception. Otherwise, the code will
compile and run until a runtime exception (or error) is thrown, at which point it will stop and indicate there was an error or exception.

2--Does the code still compile?


see above.

3--What's the difference between use of the keywords "throws" & "throw"? I always think "throws" is actually throwing the exception


'throw' actually throws the exception. You use it like 'throw new Exception()', or say you are in a catch
block where you already have a reference to the exception in scope, you could 'throw io' where 'io' is a reference
to IOException.

'throws' is the keyword used to declare an exception in a method signature:
void makeStream(File f) throws IOException { ... }

4--Any good tips for nailing down Exceptions? The questions in the K&B SCJP6 book are pretty tricky (I can only imagine what the exam is going to be like).


k&b is all you'll need. Focus on the rules presented at first, not on the tricky questions posed. Make sure you can
understand the concepts of each rule and try writing some code that incorporates those concepts. Once you
understand the basics, you should be able to successfully analyze all the questions.

SCJP 88% | SCWCD 84%
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I can only imagine what the exam is going to be like


I'd say the K&B questions are very indicative of the scope and difficulty on the actual SCJP exam. Get them down pat, and you'll ace that section!

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Throwing Exceptions from a Java Newbie