Java Exception Handling
&
Applets
Presented By
Tanmoy Roy
1
Roll : 34801216001
Content
 Introduction
 Exception Handling
 Exceptions in Java
 Hierarchy of Java Exception classes
 Types of Exceptions
 Exception Handling Java
 Java Applet
 What is Applet?
 Advantage & Drawback of Applet
 Some Important Points on Java Applet
 Lifecycle of Java Applet
2
Some Interesting Facts about Java
 The initial name of java was “Oak”. it had been modified to Java by Sun’s
marketing department once they found that name was already registered for a
computer company.
 Java is the second preferred language and is incredibly popular among the
developers.
 Java does not support the concept of pointer.
 Java is used by 95% of the companies as their primary language. Which is much
more than C and other languages.
3
Introduction
 The Exception Handling in Java is one amongst the powerful mechanism to
handle the runtime errors so traditional flow of the program will be maintained.
 Applet is a special type of program that is embedded in the webpage to generate
the dynamic content.
4
Exception Handling
5
Exceptions in Java
 An exception is an unwanted or unexpected event, which occurs during the
execution of a program i.e. at run time, that disrupts the normal flow of the
program’s instructions.
6
public class A{
public static void main(String args[])
{
int a=2, b=0, c;
c=a/b; //Exception
System.out.println(c);
}
}
Hierarchy of Java
Exception classes
7
Types of Exceptions
8
The classes which directly inherit Throwable class
except RuntimeException and Error are known as
checked exceptions
The classes which inherit RuntimeException are
known as unchecked exceptions
These are not exceptions at all, but problems that
arise beyond the control of the user or the
programmer.
Types of
Exceptions
9
unchecked exceptions
checked exceptions
Error
Exception Handling Java
10
 Exception Handling mechanism follows a flow which is depicted in the below figure. But if an
exception is not handled, it may lead to a system failure. That is why handling an exception is
very important.
Exception Handling Java
Keyword Description
try The "try" keyword is used to specify a block where we
should place exception code. The try block must be
followed by either catch or finally.
catch The "catch" block is used to handle the exception. It
must be preceded by try block
finally The "finally" block is used to execute the important code
of the program. It is executed whether an exception is
handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It
doesn't throw an exception.
11
Exception Handling Java(Example)
package ExceptionHandling;
public class Exception {
public static void main(String args[]) {
try {
int c=100/0;
}catch(ArithmeticException e) {
System.out.println("Exception caught");
}
}
}
12
Java Applet
13
What is Applet?
 An applet is a Java program that runs in a Web browser.
 An applet can be a fully functional Java application because it has the entire Java
API at its disposal.
14
Advantage & Drawback of Applet
 Advantage of Applet
 It works at client side so less response time.
 Secured
 It can be executed by browsers running under many platforms, including Linux,
Windows, Mac OS etc.
 Drawback of Applet
 Plugin is required at client browser to execute applet.
15
Some Important Points on Java Applet
 All applets are sub-classes (either directly or indirectly) of java.applet.Applet
class.
 Applets are not stand-alone programs. Instead, they run within either a web
browser or an applet viewer. JDK provides a standard applet viewer tool called
applet viewer.
 In general, execution of an applet does not begin at main() method.
 Output of an applet window is not performed by System.out.println(). Rather it is
handled with various AWT methods, such as drawString().
16
Lifecycle of Java Applet
17
Lifecycle methods for Applet
18
init( )
destroy( )
start( )
paint( )
stop( )
Lifecycle methods for Applet(Cont.)
19
init( )
destroy( )
start( )
paint( )
stop( )
is used to initialized the Applet. It is invoked only once.
is used to destroy the Applet. It is invoked only once.
is invoked after the init() method or browser is maximized. It is used to start the Applet
is used to paint the Applet. It provides Graphics class object.
is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
A "Hello, World" Applet
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);
}
}
20
<html>
<title>The Hello, World Applet</title>
<hr>
<applet code = "HelloWorldApplet.class"
width = "320" height = "120">
</applet>
<hr>
</html>
HelloWorldApplet.java HelloWorldApplet.html
Conclusion
 Exceptions are used to improve error efficiency.
 The Exception Handling in Java is one of the powerful mechanism to handle
the runtime errors.
 Applet is a Java program that can be embedded into a web page. It runs inside
the web browser and works at client side.
21
References
 Head First Java, 2nd Edition [Book] - O'Reilly Media
 http://javatpoint.com/java-tutorial
 https://www.geeksforgeeks.org/java/
 http://www.programmingtutorials.com/java.aspx
22
Thank You
23

Java Exception Handling and Applets

  • 1.
    Java Exception Handling & Applets PresentedBy Tanmoy Roy 1 Roll : 34801216001
  • 2.
    Content  Introduction  ExceptionHandling  Exceptions in Java  Hierarchy of Java Exception classes  Types of Exceptions  Exception Handling Java  Java Applet  What is Applet?  Advantage & Drawback of Applet  Some Important Points on Java Applet  Lifecycle of Java Applet 2
  • 3.
    Some Interesting Factsabout Java  The initial name of java was “Oak”. it had been modified to Java by Sun’s marketing department once they found that name was already registered for a computer company.  Java is the second preferred language and is incredibly popular among the developers.  Java does not support the concept of pointer.  Java is used by 95% of the companies as their primary language. Which is much more than C and other languages. 3
  • 4.
    Introduction  The ExceptionHandling in Java is one amongst the powerful mechanism to handle the runtime errors so traditional flow of the program will be maintained.  Applet is a special type of program that is embedded in the webpage to generate the dynamic content. 4
  • 5.
  • 6.
    Exceptions in Java An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e. at run time, that disrupts the normal flow of the program’s instructions. 6 public class A{ public static void main(String args[]) { int a=2, b=0, c; c=a/b; //Exception System.out.println(c); } }
  • 7.
  • 8.
    Types of Exceptions 8 Theclasses which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions The classes which inherit RuntimeException are known as unchecked exceptions These are not exceptions at all, but problems that arise beyond the control of the user or the programmer.
  • 9.
  • 10.
    Exception Handling Java 10 Exception Handling mechanism follows a flow which is depicted in the below figure. But if an exception is not handled, it may lead to a system failure. That is why handling an exception is very important.
  • 11.
    Exception Handling Java KeywordDescription try The "try" keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. catch The "catch" block is used to handle the exception. It must be preceded by try block finally The "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not. throw The "throw" keyword is used to throw an exception. throws The "throws" keyword is used to declare exceptions. It doesn't throw an exception. 11
  • 12.
    Exception Handling Java(Example) packageExceptionHandling; public class Exception { public static void main(String args[]) { try { int c=100/0; }catch(ArithmeticException e) { System.out.println("Exception caught"); } } } 12
  • 13.
  • 14.
    What is Applet? An applet is a Java program that runs in a Web browser.  An applet can be a fully functional Java application because it has the entire Java API at its disposal. 14
  • 15.
    Advantage & Drawbackof Applet  Advantage of Applet  It works at client side so less response time.  Secured  It can be executed by browsers running under many platforms, including Linux, Windows, Mac OS etc.  Drawback of Applet  Plugin is required at client browser to execute applet. 15
  • 16.
    Some Important Pointson Java Applet  All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.  Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.  In general, execution of an applet does not begin at main() method.  Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString(). 16
  • 17.
  • 18.
    Lifecycle methods forApplet 18 init( ) destroy( ) start( ) paint( ) stop( )
  • 19.
    Lifecycle methods forApplet(Cont.) 19 init( ) destroy( ) start( ) paint( ) stop( ) is used to initialized the Applet. It is invoked only once. is used to destroy the Applet. It is invoked only once. is invoked after the init() method or browser is maximized. It is used to start the Applet is used to paint the Applet. It provides Graphics class object. is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
  • 20.
    A "Hello, World"Applet import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello World", 25, 50); } } 20 <html> <title>The Hello, World Applet</title> <hr> <applet code = "HelloWorldApplet.class" width = "320" height = "120"> </applet> <hr> </html> HelloWorldApplet.java HelloWorldApplet.html
  • 21.
    Conclusion  Exceptions areused to improve error efficiency.  The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors.  Applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. 21
  • 22.
    References  Head FirstJava, 2nd Edition [Book] - O'Reilly Media  http://javatpoint.com/java-tutorial  https://www.geeksforgeeks.org/java/  http://www.programmingtutorials.com/java.aspx 22
  • 23.