• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Throwing Exceptions from a Java Newbie

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic