0% found this document useful (0 votes)
9 views

Minor Exp 4

hJaaakhd

Uploaded by

chudjisarvesh20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Minor Exp 4

hJaaakhd

Uploaded by

chudjisarvesh20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(NBA Accredited)

Department of Information Technology


Academic Year: 2024-25 Name of Student:
Semester: III Student ID:
Class / Branch/ Div: SE- IT A Roll No.
Subject: CPPL Date of Submission:
Name of Instructor: Prof. Rucha Kulkarni

Experiment No 4

Aim : To Implement a program in C++ and JAVA to understand Exception handling and Garbage
collection with the syntactic differences between the two languages

Objectives: To understand how exception handling and garbage collection differ in C++ and Java.

Prerequisites : It is required that students should have basic knowledge of


object oriented programming concepts.

Software used : CodeBlocks version 17.12, VSCode 1.47.2 /IntelliJ IDEA 2020.2.3

Problem Statement : To Implement a program in C++ and JAVA to understand Exception handling
and Garbage collection with the syntactic differences between the two languages

Theory:

Exception handling & garbage collection in C++ vs Java

Exception handling in java Exception handling in C++

Only throwable objects can be thrown as objects. All types can be thrown as exception

In java, finally is a block that is executed after try In C++ there is no existence of finally block
catch block for cleaning up.

A new keyword throws is used to list exceptions Throw keyword is used to list exceptions thrown
thrown by a function. by a function.

Both checked and unchecked exceptions are Only unchecked exceptions are present.
present.

Department of Information Technology | APSIT


PARSHVANATH CHARITABLE TRUST'S

A. P. SHAH INSTITUTE OF TECHNOLOGY


(NBA Accredited)

1. Exception handling in Java is different because there are no destructors. A finally clause can be
added to force execution of statements that perform necessary cleanup. All exceptions in Java are
inherited from the base class Throwable, so you’re guaranteed a common interface.

public void f(Obj b) throws IOException {

myresource mr = b.createResource();

try { mr.UseResource(); } catch (MyException e) { // handle my

exception } catch (Throwable e) { // handle all other exceptions }

finally { mr.dispose(); // special cleanup }

2. Exception specifications in Java are vastly superior to those in C++. Instead of the C++ approach of
calling a function at run-time when the wrong exception is thrown, Java exception specifications are
checked and enforced at compile-time. In addition, overridden methods must conform to the exception
specification of the base-class version of that method: they can throw the specified exceptions, or
exceptions derived from those. This provides much more robust exception-handling code.

3. If the access to a Java handle fails, an exception is thrown. This test doesn’t have to occur right
before the use of a handle; the Java specification just says that the exception must somehow be thrown.
Many C++ runtime systems can also throw exceptions for bad pointers.

4. Generally, Java is more robust, via: – Object handles initialized to null (a keyword)
– Handles are always checked and exceptions are thrown for failures
– All array accesses are checked for bounds violations
– Automatic garbage collection prevents memory leaks
– Clean, relatively fool-proof exception handling
– Simple language support for multi-threading
– Bytecode verification of network applet.

Conclusion- Java Exceptions are great way of handling exceptions. It ensures program integrity in any
condition. As a programmer we should always try to use exceptions as it makes code more reliable.

Department of Information Technology | APSIT

You might also like