0% found this document useful (0 votes)
56 views4 pages

Java Exception Handling Overview

The document discusses Java exception handling concepts and provides 5 programming questions to test exception handling knowledge. It covers topics like checked vs unchecked exceptions, exception vs error hierarchy, throw vs throws keywords, and how to write custom exceptions. The questions demonstrate try-catch blocks, exception propagation, and finally blocks.

Uploaded by

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

Java Exception Handling Overview

The document discusses Java exception handling concepts and provides 5 programming questions to test exception handling knowledge. It covers topics like checked vs unchecked exceptions, exception vs error hierarchy, throw vs throws keywords, and how to write custom exceptions. The questions demonstrate try-catch blocks, exception propagation, and finally blocks.

Uploaded by

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

DAY10:

------
[Link]
[Link] and throws
[Link] define exception

QUESTIONS(Theory)
-------------------

[Link] is Exception?
[Link] about types of Expection?
[Link] between checked expection and unchecked expection?
[Link] are the differences between exception and error?
[Link] is the super class for Exception and Error?
[Link] are defined in which java package
[Link] is throw keyword in java?
[Link] we have try block without catch block?
[Link] we write multiple catch blocks under single try block?
[Link] to write user defined exception or custom exception in java?
[Link] are the different ways to print exception message on console?
[Link] are the differences between final finally and finalize in java?
[Link] we write return statement in try and catch blocks?
[Link] we write return statement in finally block?
[Link] are the differences between throw and throws?
[Link] are the Exception Handling Keywords in Java?
[Link] Java Exception Hierarchy?
[Link] to create custom Exception?

QUESTIONS(Programs)
---------------------------
QUESTION 1:
------------
Description : Find the output for the program:

public class Test


{
public static void main(String[] args)
{
try
{
[Link]("1");
int sum = 9 / 0;
[Link]("2");
}
catch(ArithmeticException e)
{
[Link]("3");
}
catch(Exception e)
{
[Link]("4");
}
finally
{
[Link]("5");
}
}
}
QUESTION 2:
------------
Description : Find the output for the program:

public class Test


{
private void m1()
{
m2();
[Link]("1");
}
private void m2()
{
m3();
[Link]("2");
}
private void m3()
{
[Link]("3");
try
{
int sum = 4/0;
[Link]("4");
}
catch(ArithmeticException e)
{
[Link]("5");
}

[Link]("7");
}
public static void main(String[] args)
{
Test obj = new Test();
obj.m1();
}
}

QUESTION 3:
------------
Description : Find the output for the program:

public class Test


{
public static void main(String[] args)
{
try
{
[Link]("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
[Link]("2");
[Link](0);
}
finally
{
[Link]("3");
}
[Link]("4");
}
}

QUESTION 4:
------------
Description : Find the output for the program:
public class Test
{
public static void main(String[] args)
{
try
{
[Link]("1");
int data = 5 / 0;
}
catch(ArithmeticException e)
{
Throwable obj = new Throwable("Sample");
try
{
throw obj;
}
catch (Throwable e1)
{
[Link]("8");
}
}
finally
{
[Link]("3");
}
[Link]("4");
}
}

QUESTION 5:
------------
Description : Find the output for the program:

import [Link];
import [Link];

public class Test


{
public static void main(String[] args)
{
try
{
[Link]("1");
int value = 10 / 0;
throw new IOException();
}
catch(EOFException e)
{
[Link]("2");
}
catch(ArithmeticException e)
{
[Link]("3");
}
catch(NullPointerException e)
{
[Link]("4");
}
catch(IOException e)
{
[Link]("5");
}
catch(Exception e)
{
[Link]("6");
}
}
}

Common questions

Powered by AI

User-defined exceptions are exceptions defined by users to handle specific conditions that aren't covered by Java's built-in exceptions. They can be implemented by extending the Exception class or its subclasses. By creating a new class that inherits from Exception and implementing a constructor that calls the superclass constructor, a developer creates context-specific exceptions providing detailed error information tailored to specific application needs.

In Java, 'error' refers to serious problems typically outside the application's scope to recover, such as hardware failures or memory exhaustion. They differ from exceptions in that they represent critical system-level issues, while exceptions are recoverable conditions that applications might handle, such as input/output errors. Errors are subclasses of Error, not Exception, illustrating their fundamental and systemic nature.

In Java, 'final' is an access modifier applicable to classes, methods, and variables, indicating no further modification. 'finally' is a block associated with try-catch to execute code post exception handling regardless of exception occurrence. 'finalize' is a method used by GC to perform cleanup before object destruction. These concepts play distinct roles: 'final' restricts modification, 'finally' ensures code execution post-try-catch, and 'finalize' aids in resource release.

An 'Exception' in Java is an event that disrupts the normal flow of the program's instructions. It is significant because it provides a mechanism to handle runtime errors, allowing a developer to control the flow of the program in case of exceptions and ensuring robust and error-free execution. Exceptions help in debugging and provide meaningful error messages to the user.

The 'throw' keyword in Java is used to explicitly throw an exception from a method or any block of code, while 'throws' is used in the method signature to declare that this method can throw certain exceptions, thus warning the caller of the method to handle or propagate the exception. 'throw' is followed by an instance of an exception class, whereas 'throws' is followed by exception class names.

Yes, in Java, a try block can exist without a catch block if it is accompanied by a finally block. The structure would be a try block followed directly by a finally block, which is executed regardless of whether an exception is thrown, ensuring certain tasks are completed, like resource deallocation.

Java Exception Hierarchy organizes exceptions in a tree structure with Throwable as the root class. Throwable has two direct subclasses, Error and Exception. Errors are serious issues beyond application control (e.g., OutOfMemoryError), while Exception includes all issues application should attempt to handle. The hierarchy's relevance lies in its structured approach to categorize and manage exceptions, allowing developers to target specific exception handling, propagate exceptions appropriately, and create reliable error-handling mechanisms.

A finally block might not execute if the JVM exits during try/catch execution, such as by System.exit(), or if the thread is killed. Although rare, it can lead to resource leaks or unfinished tasks. For instance, improper release of file handles or locks can occur if finally is skipped. This makes exception handling unreliable for cleanup tasks, necessitating alternate measures like try-with-resources for automatic resource management.

Checked exceptions are exceptions that are checked at compile-time, like IOException, SQLException. They force the programmer to either handle the exception or declare it using the 'throws' keyword. Unchecked exceptions are checked at runtime, like NullPointerException, ArithmeticException. They do not require explicit handling or declaration and usually indicate programming bugs.

Multiple catch blocks can be placed after a single try block to handle different types of exceptions separately. Upon an exception occurrence, the program checks each catch block in order from top to bottom. The first catch block matching the exception type executes, with all subsequent catch blocks being skipped. This allows specific exception handling, improving error resolution accuracy.

You might also like