473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why do we need to put the Overridden method in a try and catch block

Why do we need to put the Overridden method in a try and catch block if the overridden method does not throw any exception.

The code goes here....
class Utils{
int getHeight(String x) throws Exception{
return 8;
}
}

class Person extends Utils{
int getHeight(String arg){
return Integer.parseInt(arg);
}

public static void main(String arg[]){
Utils u = new Person();
try{
System.out.println( u.getHeight(arg[0]) ); // Question asked below.
}catch(Exception exp){
exp.printStackTrace();
}
}
}

why is u.getHeight(arg[0]) API is put under a try/catch block..even though here the sub-class getHeight() API is called and which does not throw any exception...
Oct 10 '07 #1
2 1555
dmjpro
2,476 2GB
Why do we need to put the Overridden method in a try and catch block if the overridden method does not throw any exception.

The code goes here....
class Utils{
int getHeight(String x) throws Exception{
return 8;
}
}

class Person extends Utils{
int getHeight(String arg){
return Integer.parseInt(arg);
}

public static void main(String arg[]){
Utils u = new Person();
try{
System.out.println( u.getHeight(arg[0]) ); // Question asked below.
}catch(Exception exp){
exp.printStackTrace();
}
}
}

why is u.getHeight(arg[0]) API is put under a try/catch block..even though here the sub-class getHeight() API is called and which does not throw any exception...
Because during Compile Time "u.getHeight" known as the method of "Utils", not of "Person'.
That's why Compiler tells that.

Debasis Jana
Oct 10 '07 #2
JosAH
11,448 Expert 8TB
In the Java Article section there's an article about the Liskov Substitution Principle
that explains why this is necessary.

kind regards,

Jos
Oct 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Milan Gornik | last post by:
Hello guys, I consulted both MSDN and Borland Builder's documentation to try to find something about this, but I haven't found it. I saw that both VC++ and Builder support different kinds of...
4
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
2
by: John | last post by:
Hi, I use try, catch and finallay to control Sql connection, and run into 1 problem. Some of the sql errors, I don't want the program flow to go to catch block. Here is my code snippet: ...
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
2
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.