• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
m pap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
try
{
int i = 0;
int j = 5;
int k;
k = j / i;
}
catch (ArithmeticException e)
{
System.out.println("Division by zero error.");
}
finally
{
//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.
-Dale
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this: http://java.sun.com/docs/books/tutorial/essential/exceptions/try.html
 
m pap
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.
one more question. i was coming across too many while loops in my code and was told to use exceptions...now 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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m,
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.
Thanks,
Cindy
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic