0% found this document useful (0 votes)
10 views15 pages

L31 finally, throws keyword

The document explains the use of 'throw' and 'throws' in exception handling in Java, detailing how to explicitly throw exceptions and how to declare methods that can throw exceptions. It also covers the 'finally' block, which executes after a try/catch block, regardless of whether an exception was thrown. Additionally, it includes examples of creating custom exceptions and demonstrates how exceptions can be caught and handled.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

L31 finally, throws keyword

The document explains the use of 'throw' and 'throws' in exception handling in Java, detailing how to explicitly throw exceptions and how to declare methods that can throw exceptions. It also covers the 'finally' block, which executes after a try/catch block, regardless of whether an exception was thrown. Additionally, it includes examples of creating custom exceptions and demonstrates how exceptions can be caught and handled.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

finally and throws

Keywords
throw

It is possible to throw an exception explicitly, using the throw


statement.

The general form of throw is shown here:

throw ThrowableInstance;
throws

• If a method is capable of causing an exception that it does not handle, it must


specify this behavior so that callers of the method can guard themselves
against that exception.

• We can do this by including a throws clause in the method’s declaration.

• A throws clause lists the types of exceptions that a method might throw.

• Necessary for all exceptions, except those of type Error or RunTimeException.


4
general form of a method declaration that includes a throws clause:

type method-name(parameter-list) throws exception-list


{
// body of method
}

exception-list is a comma-separated list of the exceptions that a method


can throw. 5
// This program contains an error and will not compile.
TestIface
{ void m()
{ throw new IOException("device error");//checked exception
}

public static void main(String args[])


{
TestIface obj=new TestIface();
obj.m();
System.out.println("normal flow...");
}
}

6
import java.io.IOException;
TestIface
{
void m() throws IOException
{
throw new IOException("device error");//checked exception
}
public static void main(String args[])
{
Here is the output generated by running this
try example program:
{
TestIface obj=new TestIface(); Caught exception: device error
obj.m();
System.out.println("normal flow...");
}
7

catch(IOException e) {System.out.println("Caught exception: " +


e.getMessage()); }
finally
 finally executes a block of code that will be executed after a try/catch block has
completed and before the code following the try/catch block.

 The finally block will execute whether or not an exception is thrown.

 If an exception is thrown, the finally block will execute even if no catch statement
matches the exception.

 Used to perform certain house-keeping operations such as closing files and


releasing system resources.
8
class prg2
{ public static void main(String args[])
{
try
{ int a = 8/2;
}
catch(Exception e) finally
{ successful
System.out.println("error"+e);
}
finally
{
System.out.println("finally");
}
System.out.println(“successful");
}
} 9
class prg2
{ public static void main(String args[])
{ try
{
int a = 8/0;
}
error java.lang.ArithmeticException: / by
catch(Exception e)
zero
{ finally
successful
System.out.println("error"+e);
}
finally
{
System.out.println("finally");
}
System.out.println(“successful");
}
} 10
class FinallyDemo1
{ static void procA()
{ try { System.out.println("inside procA");
throw new RuntimeException("demo"); }
finally
{ System.out.println("procA's finally"); }
} inside procA
procA’s finally
public static void main(String args[]) Exception caught
{ try { procA(); }

catch (Exception e)
{ System.out.println("Exception caught");
}
} }
11
class FinallyDemo
{
static void procB()
{ try
inside procB
{ System.out.println("inside procB");
procB’s finally
return;
}
finally { System.out.println("procB's finally"); }
}
public static void main(String args[])
{
procB();
}
}
12
class FinallyDemo
{ static void procC()
{ try
{ System.out.println("inside procC");
}
finally
{ System.out.println("procC's finally");
} inside procC
procC’s finally
}
public static void main(String args[])
{
procC();
}
}
13
Creating our own exception
Subclasses
class MyException extends Exception
{ int detail;
MyException(int a)
{ detail = a;
}

public String toString()


{ return "MyException[" + detail + "]";
}
}

14
class ExceptionDemo
{ static void compute(int a) throws MyException
{ System.out.println("Called compute(" + a +
")");
if(a > 10)
throw new MyException(a);
System.out.println("Normal exit");
} Called compute(1)
public static void main(String args[]) Normal exit
{ try Called compute(20)
{ compute(1); Caught MyException[20]
compute(20);
}
catch (MyException e)
{ System.out.println("Caught " + e);
}
}
} 15
Throwable

Exception Error

Error : due to lack of resource


Virus attack –JVM crash
Exception : caused by programs. heap memory
are recoverable Not recoverable

You might also like