• 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

try-catch problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Peros Kalogerakos
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic