Try buffer thingyY?

Hi guys, iv'e been reading some of the posts and i see this a lot.

try{

//bunch of code

}

Is this like a do while loop, or it always executes?

Any explanation would help please.

MS

[348 byte] By [Progr@mera] at [2007-11-26 22:29:17]
# 1
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
jverda at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 2

Hey Jverd thanks for the link but i still got a couple of questions.

1. Should i use the try block from the beggining of the code to the end because i wouldn't know when there would be an error, or is there a specific place to use try?

for example if this is my code should i always do this?

public static main void--

{

try{

//my whole program

}

If not then during which part of my code should i use it?

P.S. sorry to be pestering you but they haven't taught us this yet in school im jsut curious why you guys write it.

Thanks

MS

Progr@mera at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 3

Sure you can wrap your entire program in a try statement but I think that is lazy and loathesome. You should aim to only wrap the section of code that will cause an exception. How do you know when an exception occurs? You could just compile and the error message will tell you that you haven't handled an exception or you could read about the method you are calling in the API and it will tell you what kind of exception it throws.

try {

callMethodThatThrowsException();

} catch(NastyException e) {

// handle exception

}

public void callMethodThatThrowsException() throws NastyException {

throw new NastyException();

}

floundera at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 4
Your compiler will tell you when you need to catch an exception.
CaptainMorgan08a at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 5
Once again all of you are very helpful and that is 1 less thing i need to worry about when i program. I will let them compiler throw the error and then fix it!thanks all and until next time!,MS
Progr@mera at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 6
I wouldn't rely on the compiler. I would read the API and become familiar with the method and Exception.
floundera at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...
# 7
k, thanks
Progr@mera at 2007-7-10 11:33:11 > top of Java-index,Java Essentials,New To Java...