ExceptionHandling with MethodOverriding in Java

Last updated on Dec 23 2022
Prabhas Ramanathan

There are many rules if we talk about methodoverriding with exception handling. The Rules are as follows:

Table of Contents

• If the superclass method does not declare an exception

o If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

• If the superclass method declares an exception

o If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

If the superclass method does not declare an exception

1) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

1. import java.io.*; 
2. class Parent{ 
3. void msg(){System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild extends Parent{ 
7. void msg()throws IOException{ 
8. System.out.println("TestExceptionChild"); 
9. } 
10. public static void main(String args[]){ 
11. Parent p=new TestExceptionChild(); 
12. p.msg(); 
13. } 
14. }

 

Output:Compile Time Error

2) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.

1. import java.io.*; 
2. class Parent{ 
3. void msg(){System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild1 extends Parent{ 
7. void msg()throws ArithmeticException{ 
8. System.out.println("child"); 
9. } 
10. public static void main(String args[]){ 
11. Parent p=new TestExceptionChild1(); 
12. p.msg(); 
13. } 
14. }

 

Output:child
If the superclass method declares an exception
1) Rule: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception

1. import java.io.*; 
2. class Parent{ 
3. void msg()throws ArithmeticException{System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild2 extends Parent{ 
7. void msg()throws Exception{System.out.println("child");} 
8. 
9. public static void main(String args[]){ 
10. Parent p=new TestExceptionChild2(); 
11. try{ 
12. p.msg(); 
13. }catch(Exception e){} 
14. } 
15. }

 

Output:Compile Time Error

 

Example in case subclass overridden method declares same exception

1. import java.io.*; 
2. class Parent{ 
3. void msg()throws Exception{System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild3 extends Parent{ 
7. void msg()throws Exception{System.out.println("child");} 
8. 
9. public static void main(String args[]){ 
10. Parent p=new TestExceptionChild3(); 
11. try{ 
12. p.msg(); 
13. }catch(Exception e){} 
14. } 
15. }

 

Output:child

 

Example in case subclass overridden method declares subclass exception

1. import java.io.*; 
2. class Parent{ 
3. void msg()throws Exception{System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild4 extends Parent{ 
7. void msg()throws ArithmeticException{System.out.println("child");} 
8. 
9. public static void main(String args[]){ 
10. Parent p=new TestExceptionChild4(); 
11. try{ 
12. p.msg(); 
13. }catch(Exception e){} 
14. } 
15. }

 

Output:child

 

Example in case subclass overridden method declares no exception

1. import java.io.*; 
2. class Parent{ 
3. void msg()throws Exception{System.out.println("parent");} 
4. } 
5. 
6. class TestExceptionChild5 extends Parent{ 
7. void msg(){System.out.println("child");} 
8. 
9. public static void main(String args[]){ 
10. Parent p=new TestExceptionChild5(); 
11. try{ 
12. p.msg(); 
13. }catch(Exception e){} 
14. } 
15. }

 

Output:child

Difference between throw and throws in Java

There are many differences between throw and throws keywords. A list of differences between throw and throws are given below:

No. throw throws
1) Java throw keyword is used to explicitly throw an exception. Java throws keyword is used to declare an exception.
2) Checked exception cannot be propagated using throw only. Checked exception can be propagated with throws.
3) Throw is followed by an instance. Throws is followed by class.
4) Throw is used within the method. Throws is used with the method signature.
5) You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.

 

Java throw example

1. void m(){ 
2. throw new ArithmeticException("sorry"); 
3. }

Java throws example

1. void m()throws ArithmeticException{ 
2. //method code 
3. }

Java throw and throws example

1. void m()throws ArithmeticException{ 
2. throw new ArithmeticException("sorry"); 
3. }

Difference between final, finally and finalize

No. final finally finalize
1) Final is used to apply restrictions on class, method and variable. Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed. Finally is used to place important code, it will be executed whether exception is handled or not. Finalize is used to perform clean up processing just before object is garbage collected.
2) Final is a keyword. Finally is a block. Finalize is a method.

Java final example

1. class FinalExample{ 
2. public static void main(String[] args){ 
3. final int x=100; 
4. x=200;//Compile Time Error 
5. }}

Java finally example

1. class FinallyExample{ 
2. public static void main(String[] args){ 
3. try{ 
4. int x=300; 
5. }catch(Exception e){System.out.println(e);} 
6. finally{System.out.println("finally block is executed");} 
7. }}

Java finalize example

1. class FinalizeExample{ 
2. public void finalize(){System.out.println("finalize called");} 
3. public static void main(String[] args){ 
4. FinalizeExample f1=new FinalizeExample(); 
5. FinalizeExample f2=new FinalizeExample(); 
6. f1=null; 
7. f2=null; 
8. System.gc(); 
9. }}

So, this brings us to the end of blog. This Tecklearn ‘ExceptionHandling with MethodOverrding in Java’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page

• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "ExceptionHandling with MethodOverriding in Java"

Leave a Message

Your email address will not be published. Required fields are marked *