exception handling examples

Upload: vishwanath-c

Post on 23-Feb-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Exception Handling Examples

    1/42

    Exception Handling Examples

    Exception in java with Example Program

    1. Exception

    An exception is a problem that arises during the execution of a program

    Exception is a runtime error

    An exception can occur for many different reasons, including the following:

    A user has entered invalid data.

    A file that needs to be opened cannot be found.

    A network connection has been lost in the middle of communications, or the JVM has run out of memory.

    Types of error:

    Compile time error

    Compile time error is any type of error that prevent a java program compile like a syntax error, a class not found, a bad file name for the defined

    class, and so on.

    Run time error

    A runtime error means an error which happens, while the program is running. To deal with this kind of errors java define Exceptions. Exceptions

    are objects represents an abnormal condition in the flow of the program. It can be either checked or unchecked.

    1.2 Types of exception

    Checked Exceptions

    Errors

    Runtime Exceptions

    Checked Exception( handled at compile time.)

    Environmental error that cannot necessarily be detected by testing; e.g. disk full, broken socket, database unavailable, etc. The Exception problemcan be overcome by implementing Try-Catch block.

    Error

    Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc

    Runtime Exception(Unchecked Exception)

    Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument, etc.

    The Compiler never checks the Unchecked exception during the program compilation. For example, Dividing any number by zero is an unchecked

    exception.

    1.3Exception Hierarchy

    1.4 Exception management

    Exception can be managed through five keywords in java

    1.try

    2.catch

    3.finally

    4.throw

    5.throws

    try-used to keep the code that going to throw error

    catch-used to catch and process the error thrown by try block

    example for try /catch block:

  • 7/24/2019 Exception Handling Examples

    2/42

    ?

    1

    2

    3

    4

    56

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    classSam

    {

    publicstaticvoidmain(String args[])

    {

    intk=0;

    intmsg;

    String s1=args[0];

    String s2=args[1];

    inti=IntegerparseInt(s1);

    int!=IntegerparseInt(s2);

    tr"

    {

    k=i#!;

    S"stemoutprintln(k);

    $

    catc%(&'ception e)

    {

    S"stemoutprintln(error);

    $

    $

    finally-A finally block of code always executes, whether or not an exception has occurred. Finally used for avoid resource leakage .

    The finally clause is executed after a try-catch block has been executed .

    ?

    1

    2

    3

    4

    5

    6

    tr"{##*rotected code$catc%(&'ception+"pe1 e1){

    ##,atc% block$catc%(&'ception+"pe2 e2){

    http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/
  • 7/24/2019 Exception Handling Examples

    3/42

    7

    8

    9

    10

    11

    1213

    14

    15

    16

    ##,atc% block$catc%(&'ception+"pe- e-){##,atc% block

    $.inall"{##+%e .inall" block al/a"s e'ecutes$

    Example for finally:

    ?

    1,onnection conn; ## opened be.ore at t%e beginning o.t%e met%od

    ?

    1

    2

    3

    4

    5

    6

    7

    tr"{## e'ecute a uer"$ catc%(S3&'ception e) {## %andles t%e e'ception dispal"ing a message /%atever$ .inall"{connclose();$

    throw-throwis used to actually throw the exception

    throws-throwsis declarative for the method. They are not interchangeable.

    The following code shows how to handle multiple exceptions using thethrowskeyword.

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    2021

    publicclass&'ample{ publicvoide'ception&'ample() { tr" { ## statements c%eck(); $ catc%(&'ception e)

    { ##statements $ $ ## multiple e'ceptions separated b" a comma

    voidc%eck() t%ro/s4ull*ointer&'ception4egative5rra"Si6e&'ception { i.(.lag 7 0) t%ro/ne/4ull*ointer&'ception(); i.(arrsi6e 7 0) t%ro/ne/4egative5rra"Si6e&'ception();

    $$

    http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/http://candidjava.com/exception-in-java-with-example-program/
  • 7/24/2019 Exception Handling Examples

    4/42

    22

    23

    24

    Primarykey

    This tutorial explains how to set a primary key for a table in mysql using java statement example, Load the mysql driver using Class.forName

    method and create a connection to communicate with mysql database and then create a statement object to send and receive from java code and

    use executeUpdate to execute the query. If we wants to change a column constrain to primary key using java use this tutorial

    Java code:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    import!avasl,onnection;

    import!avasl8river9anager;

    import!avaslS3&'ception;

    import!avaslStatement;

    publicclass*rimar"ke" {

    #::

    : param args

    :#

    publicstaticvoidmain(String[] args) {

    ## +comm"sl!dbc8river>;

    String url = >!dbc?m"sl?##local%ost#test>;

    String u4ame =>root>;

    String p/d = >root>;

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url

    http://candidjava.com/statement-primary-key-example-program-in-java-jdbc-mysql-database/http://candidjava.com/statement-primary-key-example-program-in-java-jdbc-mysql-database/
  • 7/24/2019 Exception Handling Examples

    5/42

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    u4ame p/d);

    Statement stmt = conncreateStatement();

    String sl = >5lter table student 5dd

    primar" ke" (@ollA4o)>;

    stmte'ecuteBpdate(sl);

    S"stemoutprintln(>primar"ke" added

    sucess.ull">);

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    6/42

    50

    51

    $

    Output:

    Unique statement:

    This tutorial explains how to update a column constrainto unique in a table in mysql using java statement example, Load the mysql driver using

    Class.forName method and create a connection to communicate with mysql database and then create a statement object to send and receive

    from java code and use executeUpdate to execute the query.

    Java code:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    import!avasl,onnection;

    import!avasl8river9anager;

    import!avasl@esultSet;

    import!avaslS3&'ception;

    import!avaslStatement;

    publicclassBniue {

    #::

    : param args

    :#

    publicstaticvoidmain(String[] args) {

    ## +comm"sl!dbc8river>;

    String url = >!dbc?m"sl?##local%ost#sec>;

    String u4ame =>root>;

    String p/d = >root>;

    http://candidjava.com/statement-unique-example-program-in-java-jdbc-mysql-database/http://candidjava.com/statement-unique-example-program-in-java-jdbc-mysql-database/
  • 7/24/2019 Exception Handling Examples

    7/42

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url u4ame p/d);

    Statement stmt = conncreateStatement();

    String sl = >create table student2(rollAno int 4create table sucess.ull">);

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    8/42

    45

    46

    47

    48

    49

    50

    51

    $

    $

    $

    Output:

    Create statement:

    This tutorial explains how to Create a table in mysql using java statement example, Load the mysql driver using Class.forName method and create

    a connection to communicate with mysql database and then create a statement object to send and receive from java code and use

    executeUpdate to execute the query . The following codes in this java tutorial explain how to Create a table named staff with columns as Fields,

    Type, Null, Key, Default, Extra .

    Java code:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    import!avasl,onnection;

    import!avasl8river9anager;

    import!avasl@esultSet;

    import!avaslS3&'ception;

    import!avaslStatement;

    publicclass,reate+able {

    #::

    : param args

    :#

    publicstaticvoidmain(String[] args) {

    http://candidjava.com/statement-create-table-example-program-in-java-jdbc-mysql-database/http://candidjava.com/statement-create-table-example-program-in-java-jdbc-mysql-database/
  • 7/24/2019 Exception Handling Examples

    9/42

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    ## +comm"sl!dbc8river>;

    String url = >!dbc?m"sl?##local%ost#test>;

    String u4ame =>root>;

    String p/d = >root>;

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url u4ame p/d);

    Statement stmt = conncreateStatement();

    String sl = >create table sta..(name varc%ar(2D)department

    varc%ar(D)sub!ectAname varc%ar(2D))>;

    stmte'ecuteBpdate(sl);

    S"stemoutprintln(>,reate+able sucess.ull">);

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    10/42

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    tr"{

    connclose();

    $ catc%(S3&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    11/42

    5 uer"

  • 7/24/2019 Exception Handling Examples

    12/42

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    u4ame p/d);

    Statement stmt = conncreateStatement();

    String sl = >delete .rom student /%ere

    name=HraviH>;

    stmte'ecuteBpdate(sl);

    S"stemoutprintln(>8ata deleted

    sucess.ull">);

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    13/42

    49

    50

    51

    $

    OUTPUT:

    Data deleted successfully

    SQ I!S"#T I!TO Statement:

    This tutorial explains how to insert a single row into a table in mysql using java statement example, Load the mysql driver using Class.forName

    method and create a connection to communicate with mysql database and then create a statement object to send and receive from java code and

    use executeUpdate to execute the query. If we wants to add a new record into the table or to add a new row, just use this following code to

    update the table with new record.

    create table:

    ?

    1

    2

    3

    4

    5

    createtablestudent(@ollA4o int(D)4amevarc%ar(2D)

    8epartment varc%ar(E)5ddress varc%ar(-0));

    uer"

  • 7/24/2019 Exception Handling Examples

    14/42

    Java code:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    import!avasl,onnection;

    import!avasl8river9anager;

    import!avaslS3&'ception;

    import!avaslStatement;

    publicclass,reate+able {

    #::

    : param args

    :#

    publicstaticvoidmain(String[] args) {

    ## +comm"sl!dbc8river>;

    String url = >!dbc?m"sl?##local%ost#test>;

    String u4ame =>root>;

    String p/d = >root>;

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url

    u4ame p/d);

    Statement st= conncreateStatement();

    String sl=>insert into student

    values(H101HHsures%HHcseHHc%ennaiH)>;

    ste'ecuteBpdate(sl);

    S"stemoutprintln(>8ata Inserted

    sucess.ull">);

    http://candidjava.com/statement-insert-example-program-in-java-jdbc-mysql-database/http://candidjava.com/statement-insert-example-program-in-java-jdbc-mysql-database/
  • 7/24/2019 Exception Handling Examples

    15/42

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    16/42

    Data Inserted successfully

    $lter statement:

    This tutorial explains how to alter a table in mysql using java statement example, Load the mysql driver using Class.forName method and create aconnection to communicate with mysql database and then create a statement object to send and receive from java code and use executeUpdate

    to execute the query . While creating student table there is only four columns namely Roll_NO, Name, Department, Address. The following codes

    in this java tutorial explain how to alter a table and create a new column.

    create table:

    ?

    1

    2

    3

    4

    5

    createtablestudent(@ollA4o int(D)4amevarc%ar(2D)

    8epartment varc%ar(E)5ddress varc%ar(-0));

    uer"

  • 7/24/2019 Exception Handling Examples

    17/42

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    import!avaslStatement;

    publicclass5lter {

    #::

    : param args

    :#

    publicstaticvoidmain(String[] args) {

    ## +comm"sl!dbc8river>;

    String url = >!dbc?m"sl?##local%ost#test>;

    String u4ame =>root>;

    String p/d = >root>;

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url

    u4ame p/d);

    Statement stmt = conncreateStatement();

    String sl = >5lter table student 5dd

    8ateAo.Abirt% date>;

    stmte'ecuteBpdate(sl);

    S"stemoutprintln(>table 5ltersucess.ull">);

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    18/42

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    ## +

  • 7/24/2019 Exception Handling Examples

    19/42

    Update statement:

    This tutorial explains how to update a existing row in a table in mysql using java statement example, Load the mysql driver using Class.forName

    method and create a connection to communicate with mysql database and then create a statement object to send and receive from java code and

    use executeUpdate to execute the query. Use update query to update a existing data in a table this will be helpful in modifying record in a table.

    create table:

    ?

    1

    2

    3

    4

    5

    createtablestudent(@ollA4o int(D)4amevarc%ar(2D)

    8epartment varc%ar(E)5ddress varc%ar(-0));

    uer"

  • 7/24/2019 Exception Handling Examples

    20/42

  • 7/24/2019 Exception Handling Examples

    21/42

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    connclose();

    $ catc%(S3&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    22/42

    2

    3

    4

    5

    8epartment varc%ar(E)5ddress varc%ar(-0));

    uer"

  • 7/24/2019 Exception Handling Examples

    23/42

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    ,onnection conn=null;

    tr"{

    ,lass.or4ame(8river)ne/Instance();

    conn = 8river9anagerget,onnection(url

    u4ame p/d);

    Statement stmt = conncreateStatement();

    @esultSet rs = stmte'ecuteuer"(>S&3&,+ :

    C@);

    S"stemoutprintln(>select statement

    sucess.ull" used>);

    /%ile(rsne't()) {

    String @ollAno=

    rsgetString(>@ollAno>);

    String name= rsgetString(>name>);

    String department=

    rsgetString(>department>);

    String address=

    rsgetString(>address>);

    S"stemoutprint(@ollAnoJ >Kt>);

    S"stemoutprint(nameJ >Kt>);

    S"stemoutprint(departmentJ >Kt>);

    S"stemoutprintln( addressJ >Kt>);

    $

    $ catc%(,lass4otCound&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    24/42

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    catc%(&'ception e) {

    ## +

  • 7/24/2019 Exception Handling Examples

    25/42

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    #::

    : aut%or candid!ava

    : description?Lu..eredInputStream e'ample program .or

    close() in !ava

    :#

    import!avaioCile;

    import!avaioLu..eredInputStream;

    import!avaioCileInputStream;

    import!avaioCile4otCound&'ception;

    import!avaioI&'ception /%ile reading

    t%e .ile >J ioe);

    $ .inall"{

  • 7/24/2019 Exception Handling Examples

    26/42

    29

    30

    31

    32

    33

    34

    35

    36

    tr"{

    i.(bin N= null)

    binclose();

    $ catc%(I

  • 7/24/2019 Exception Handling Examples

    27/42

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    publicstaticvoidmain(String[] args) t%ro/s&'ception

    {

    tr"{

    String s=>candid>;##get t%e string

    3ong l=ne/3ong(2OG);##get t%e long value

    S"stemoutprintln(lvaluecandid>;##get t%e string

  • 7/24/2019 Exception Handling Examples

    28/42

    11

    12

    13

    14

    15

    16

    17

    18

    S"stemoutprintln(3ongvalue

  • 7/24/2019 Exception Handling Examples

    29/42

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    {

    S"stemoutprintln(n.eget9essage());##to print t%e e'ception massage

    $

    $

    $

    class8ee ##sub class

    {

    8ee(){$

    void8ee() t%ro/s4umberCormat&'ception

    {$

    $

    output

    )*+

    !ileOutputStream example program for get!"() () in java

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    #::

    :aut%or? candid!ava

    :description? Cile

  • 7/24/2019 Exception Handling Examples

    30/42

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    Cile8escriptor .d = null;

    booleanbool = .alse;

    tr"{

    ## create ne/ .ile output stream

    .os = ne/Cile,?##testt't>);

    ## get .ile descriptor instance

    .d = .osgetC8();

    ## test i. t%e .ile is valid

    bool = .dvalid();

    ## print

    S"stemoutprint(>Is .ile validQ >J bool);

    $ catc%(&'ception e') {

    ## i. an error occurs

    e'printStack+race();

    $ .inall"{

    i.(.os N= null)

    .osclose();

    $

    $

    $

    Output

    is file valid? true

    !ileInputStream example program for s#ip(long n) in java

    1

    2

    #::

    :aut%or? candid!ava

  • 7/24/2019 Exception Handling Examples

    31/42

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    :description?

    ::#

    import!avaioI,%aracter read? >J c);

    $ catc%(&'ception e') {

    ## i. an" error occurs

    e'printStack+race();

    $ .inall"{

    ## releases all s"stem resources .rom t%e

    streams

    i.(.is N= null)

    .isclose();

    $

  • 7/24/2019 Exception Handling Examples

    32/42

    31

    32

    33

    $

    $

    Output

    Character read: E

    !ileOutputStream example program for write(int $) () in java

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    #::

    :aut%or? candid!ava

    :description? Cile);

  • 7/24/2019 Exception Handling Examples

    33/42

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    ## /rites b"te to t%e output stream

    .os/rite(b);

    ## .lus%es t%e content to t%e underl"ing

    stream

    .os.lus%();

    ## create ne/ .ile input stream

    .is = ne/CileInputStream(>,?##testt't>);

    ## read till t%e end o. t%e .ile

    /%ile((i = .isread()) N= 1) {

    ## convert integer to c%aracter

    c = (c%ar) i;

    ## prints

    S"stemoutprint(c);

    $

    $ catc%(&'ception e') {

    ## i. an error occurs

    e'printStack+race();

    $ .inall"{

    ## closes and releases s"stem resources

    .rom stream

    i.(.os N= null)

    .osclose();

    i.(.is N= null)

    .isclose();

    $

    $

    $

    Output

    B

  • 7/24/2019 Exception Handling Examples

    34/42

    !ileInputStream example program for close() in java

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    #::

    program ? CileInputStream e'ample program .or close()

    in !ava

    aut%or ? >candid>

    date ? 2R1012

    :#

    import!avaioCileInputStream;

    import!avaioI5vailable ?>Javailable);

    S"stemoutprintln(> @ead ?>J c);

    $

    $ catc%(&'ception e) {

    eprintStack+race();

  • 7/24/2019 Exception Handling Examples

    35/42

    25

    26

    27

    28

    29

    30

    31

    $ .inall"{

    i.(.is N= null) {

    .isclose();

    $

    $

    $

    $

    Output

    Available :5 Read :K

    Available :4 Read :AAvailable :3 Read :R

    Available :2 Read :T

    Available :1 Read :H

    Available :0 Read :I

    BufferedInputStream example program for s#ip(long n) in java

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    #::

    program ? Lu..eredInputStream e'ample program .or

    skip(long n) in !ava

    aut%or ? >candid>

    date ? 211112

    :#

    ##!ava code

    import!avaioLu..eredInputStream;

    import!avaioCileInputStream;

    import!avaioCilterInputStream;

    import!avaioI

  • 7/24/2019 Exception Handling Examples

    36/42

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    publicstaticvoidmain(String[] args) t%ro/s&'ception {

    InputStream is = null;

    CilterInputStream .is = null;

    inti = 0;

    c%arc;

    tr"{

    is = ne/CileInputStream(>C?#kart%i#LIStreamSkip#kart%it't>);##

    kart%it't

    ## content?kart%i

    .is = ne/Lu..eredInputStream(is);

    /%ile((i = .isread()) N= 1) {

    c = (c%ar) i;

    .isskip(-);

    S"stemoutprintln(>,%aracter read? >J c);

    $

    $ catc%(I

  • 7/24/2019 Exception Handling Examples

    37/42

    BufferedInputStream example program for reset() in java

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    #::

    : aut%or? candid!ava

    : description? in P5M5 e'ample program .or

    :#

    import!avaioLu..eredInputStream;

    import!avaioCileInputStream;

    import!avaioI,%ar ? >J (c%ar)bisread());

    S"stemoutprintln(>,%ar ? >J (c%ar)

    bisread());

  • 7/24/2019 Exception Handling Examples

    38/42

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    ## mark is set on t%e input stream

    bismark(0);

    S"stemoutprintln(>,%ar ? >J (c%ar)bisread());

    S"stemoutprintln(>reset() invoked>);

    ## reset is called

    bisreset();

    ## read and print c%aracters

    S"stemoutprintln(>,%ar ? >J (c%ar)bisread());

    S"stemoutprintln(>,%ar ? >J (c%ar)bisread());

    $ catc%(&'ception e) {

    eprintStack+race();

    $ .inall"{

    ## releases an" s"stem resources associated

    /it% t%e stream

    i.(iStream N= null)

    iStreamclose();

    i.(bis N= null)

    bisclose();

    $

    $

    $

    Output

    Char : A

    Char : B

    Char : C

    Char : D

    reset() invoked

    Char : D

    Char : E

  • 7/24/2019 Exception Handling Examples

    39/42

    %hread Example program for run()

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    ## +%read &'ample *rogram .or run()

    #::

    aut%or? ,andid!avacom

    description? +%is program runs t%e run() met%od .irst

    t%en e'ecutes t%e main met%od

    :#

    publicclass4e/ implements@unnable

    {

    publicvoidrun() ## run met%od starts %ere

    {

    tr"{

    .or(inti=0;i711;iJJ)

    {

    S"stemoutprintln(i);

    +%readsleep(1000); ##/aiting .or one second

    $

    $

    catc%(&'ception e){$

    $

    publicstaticvoidmain(String a[])

    {

    4e/ n=ne/4e/(); ## creating a ne/ ob!ect

    +%read t1=ne/+%read(n); ## creating a t%read ob!ect

    t1run(); ## starting t%e run met%od

    tr"{

  • 7/24/2019 Exception Handling Examples

    40/42

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    .or(int!=11;!721;!JJ)

    {

    S"stemoutprintln(!);

    +%readsleep(1000);

    $

    $

    catc%(&'ception e){$

    $

    $

    output:

    ,

    -

    .

    /

    +

    )

    *

    0

    1

    2-,

    --

    -.

    -/

    -+

    -)

    -*

    -0

    -1

    -2

    .,

    &ava Program used to get the pathname of the file using getPath() method

    Get Filepath program to get the pathname of the file.

    "3ample Pro&ram

    ?

    1 packageservice1;

    http://candidjava.com/java-program-used-to-get-the-pathname-of-the-file-using-getpath-method/http://candidjava.com/java-program-used-to-get-the-pathname-of-the-file-using-getpath-method/
  • 7/24/2019 Exception Handling Examples

    41/42

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    import!avaioCile;

    publicclassStrCilepat% {

    publicstaticvoidmain(String[] args) {

    Cile . = null;

    String v;

    booleanbool = .alse;

    tr"{

    ## create ne/ .ile

    . = ne/Cile(>testt't>);

    ## pat%name string .rom abstract pat%name

    v = .get*at%();

    ## true i. t%e .ile pat% e'ists

    bool = .e'ists();

    ## i. .ile e'ists

    i.(bool) {

    ## prints

    S"stemoutprint(>pat%name? >J v);

    $

    $ catc%(&'ception e) {

    ## i. an" error occurs

    eprintStack+race();

    $

  • 7/24/2019 Exception Handling Examples

    42/42

    30

    31

    32

    33

    $

    $

    Output

    pathname: test.txt

    Description

    Thejava.io.File.getPath()method converts the abstract pathname into pathname string. To separate the names in the name sequence the

    resulting string uses the default name-separator character.

    Declaration

    Following is the declaration for java.io.File.getPath() method:

    public StringgetPath()

    Parameters

    Not Applicable.

    Return Value

    The method returns pathname string form of this abstract pathname.

    Exception

    Not Applicable.