Page 385 - CITS - Computer Software Application -TT
P. 385
COMPUTER SOFTWARE APPLICATION - CITS
NewException(int y) {
x=y;
}
public String toString(){
return (“Exception value = “+x) ;
}
}
Output:
Description
In the above code, we have created two classes, i.e., UserDefinedException and NewException. The User
DefinedException has our main method, and the NewException class is our user-defined exception class,
which extends exception. In the NewException class, we create a variable x of type integer and assign a value
to it in the constructor. After assigning a value to that variable, we return the exception message.
In the UserDefinedException class, we have added a try-catch block. In the try section, we throw the exception,
i.e., NewException and pass an integer to it. The value will be passed to the NewException class and return
a message. We catch that message in the catch block and show it on the screen.
Difference Between Checked and Unchecked Exception
S.No. Checked Exception Unchecked Exception
These exceptions are just opposite to the
These exceptions are checked at compile time.
1 checked exceptions. These exceptions are not
These exceptions are handled at compile time too.
checked and handled at compile time.
These exceptions are direct subclasses of exception They are the direct subclasses of the
2
but not extended from RuntimeException class. RuntimeException class.
The code compiles without any error because
The code gives a compilation error in the case when
3 a method throws a checked exception. The compiler the exceptions escape the notice of the compiler.
These exceptions are the results of user-created
is not able to handle the exception on its own.
errors in programming logic.
These exceptions mostly occur when the probability These exceptions occur mostly due to
4
of failure is too high. programming mistakes.
Common unchecked exceptions include
Common checked exceptions include IOException,
5 ArithmeticException,InvalidClassException,
DataAccessException, InterruptedException, etc.
NullPointerException, etc.
372
CITS : IT&ITES - Computer Software Application - Lesson 101 - 108