Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Throwing Exceptions from a Java Newbie

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic