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/ 16
Java Package
• A java package is a group of similar types of classes, interfaces and sub-
packages. • Package in java can be categorized in two form, built-in package and user- defined package. • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. • Advantage of Java Package • 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. • 2) Java package provides access protection. • 3) Java package removes naming collision. package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); }} Exception Handling Exception Handling • Difference between checked and unchecked exceptions • 1) Checked Exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time. • 2) Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptionsarenotchecked at compile-timeratherthey arechecked at runtime. • 3) Error: Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionErroretc. Hierarchy of Java Exception classes Checked and UnChecked Exceptions Java try block Java catch block Solution by exception handling public class Testtrycatch2{ public static void main(String args[]){ try{ int data=50/0; }catch(ArithmeticException e){System.out.println(e);} System.out.println("rest of the code..."); }} 1. Output: Now, as displayed in the above example, rest of the code is executed i.e. rest of the code... statement is printed. Java Multi catch block Java finally block • Java finally block is a block that is used to execute important code such as closing connection, stream etc. Java finally block is always executed whether exception is handled or not. Java finally block follows try or catch block. Java throw keyword • The Java throw keyword is used to explicitly throw an exception. • We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception. We will see custom exceptions later. • The syntax of java throw keyword is given below. • 1. throw exception; Java throw keyword example In this example, we have created the validate method that takes integer value as a parameter. If the age is less than 18, we are throwing the ArithmeticException otherwise print a message welcome to vote Java throw keyword example example Java Custom Exception homework • Write a program to create user defined package and demonstrate various access modifiers. • Write a program if number is less than 10 and greater than 50 it generate the exception out of range. else it displays the square of number. • Write a program with multiple catch Statements.