File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes try-catch problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try-catch problem" Watch "try-catch problem" New topic
Author

try-catch problem

Peros Kalogerakos
Greenhorn

Joined: Jan 30, 2002
Posts: 7
Hi,
I am new to Java and trying to learn it. I wrote following code to try and simulate try-catch block.
However it doesn't throw my customized message, it just prints out the normal unhandled type of exception:
Can anyone please take a look and tell me what i did wrong - I can't see the problem?
Thank-you.
public class Test {
class MyException extends ArithmeticException {
MyException(String msg) { super(msg); }
}
public static void main(String args[]) {
Test ReTest = new Test();
try {System.out.println(ReTest.division());
} catch (MyException e) {
System.out.println(" this is the message " + e);
}
}
public int division() throws MyException {
int num1 = 10;
int num2 = 0;
int num3 = (num1/num2);
System.out.println(num1/num2);
return num3;
}
}
[ January 30, 2002: Message edited by: Peros Kalogerakos ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Peros
In your code you never actually throw a MyException.
Try this piece of code in your division method:

Or if you want to throw one of your exceptions if there is a ArithmeticException then do this:

hope that helps
[ January 30, 2002: Message edited by: Dave Vick ]

Dave
Peros Kalogerakos
Greenhorn

Joined: Jan 30, 2002
Posts: 7
Thanks man!
Ok so now I get it:
1. Even if I specify that a method THROWS an exception I need to still EXPLICITLY write code to THROW that error.
2. If I need to catch a specific type of error (e.g. Arithmetic) then I need to catch the specific error (arithmetic) but I need to throw my personal error class. (if I want my personal error msg to display).
Do you think I understand this exception business now?
Many Thanks for your reply - never knew about this website - what a treasure - I spent 3 hrs last night trying to figure this one out!!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Peros Kalogerakos:
2. If I need to catch a specific type of error (e.g. Arithmetic) then I need to catch the specific error (arithmetic) but I need to throw my personal error class. (if I want my personal error msg to display).

Hello,
You don't need to throw your personal error class, unless you really want to for some reason. The following code is an alternative:

Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Peros Kalogerakos:

1. Even if I specify that a method THROWS an exception I need to still EXPLICITLY write code to THROW that error.

Or if you call a method that can throw that Exception and don't catch it then that would also satisfy the compiler.

2. If I need to catch a specific type of error (e.g. Arithmetic) then I need to catch the specific error (arithmetic) but I need to throw my personal error class. (if I want my personal error msg to display).

You can catch the specific Exception that is thrown or a superclass of that Exception. For example, your code could also have done this:

Because MyException is an ArithmeticException it'll still be caught in the catch block.

Do you think I understand this exception business now?

Only you can answer that one.
We're glad you like the site - tell all your friends!!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
For some reason I couldn't edit my last post but here is one more thought...
Originally posted by Peros Kalogerakos:
1. Even if I specify that a method THROWS an exception I need to still EXPLICITLY write code to THROW that error.

In the example you wrote that is going to be true because you picked ArithmeticException which is unchecked so you'll never find a method that is declared to throw it. On the other hand, the compiler does not care if you declare that your method throws it and it really doesn't because it isn't even checking it.
If you tried your example and subclassed a checked exception it wouldn't have compiled because you weren't throwing the Exception.
hope that helps too
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try-catch problem