my dog learned polymorphism
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic

Exception Handling

m pap

Joined: Jun 13, 2001
Posts: 13
Can anyone explain to me the whole concept of excpetion handling...specifically trys and catchs. mostly why and how they are used.
thanks, m pap
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
The basic comcept of it is this.
When you are creating a program, there are certain problems that MAY come up. And if they DO come up, you'll want to handle them in a specific way for specific errors. If you get a math error, you may want to handle the error correctly and continue to execute. If you get a file access error, you may want to handle it entirely different. For example, if you are doing math and you divide by zero, an Arithmatic error will happen.
public static void main(String[] args)
int i = 0;
int j = 5;
int k;
k = j / i;
catch (ArithmeticException e)
System.out.println("Division by zero error.");
//Anything else that needs to be done after the try or the catch happens here
//This will fire in almost every situation
//This is useful for connections or doing anything that needs to happen EVERY time.
The finally block is used for those operations that need to happen no matter what! Regardless of if you have an error or not, before we leave this method we would want to close our connection to a database.
Thats the general reason why you want to have exceptions.
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Try this:

Please ignore post, I have no idea what I am talking about.
m pap

Joined: Jun 13, 2001
Posts: 13
one more question. i was coming across too many while loops in my code and was told to use does that break up the while loop and loops are no longer used. how do you use exceptions to fix the problem of too many loops.
thank you, m pap
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.

"JavaRanch, where the deer and the Certified play" - David O'Meara
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I don't know if using exceptions is the best way to fix having too many while loops. And I guess I would have see all these loops to understand why you have too many of them.
You can use exceptions to control your programs flow of control, but that isn't their best use. You want to keep exceptions to handling error conditions.
If you are in a while loop and you do a "throw" you will leave that loop and go to the enclosing try/catch block.
Any other questions?
I agree. Here's the link:
subject: Exception Handling
jQuery in Action, 3rd edition