How to catch multiple exceptions explain with example, JAVA Programming

Assignment Help:

How to catch multiple exceptions explain with example?

If multiple blocks match the exception type, the first block in which matches the kinds of the exception catches it.
public class HelloThere {

public static void main(String[] args) {

int repeat;

try {
// possible NumberFormatException and ArrayIndexOutOfBoundsException
repeat = Integer.parseInt(args[0]);

// possible ArithmeticException
int n = 2/repeat;

// possible StringIndexOutOfBoundsException
String s = args[0].substring(5);
}
catch (NumberFormatException e) {
// print an error message
System.err.println("Usage: java HelloThere repeat_count" );
System.err.println(
"where repeat_count is the number of times to say Hello" );
System.err.println("and given as an integer like 1 or 7" );
return;
}
catch (ArrayIndexOutOfBoundsException e) {
// pick a default value
repeat = 1;
}
catch (IndexOutOfBoundsException e) {
// ignore it
}
catch (Exception e) {
// print an error message and exit
System.err.println("Unexpected exception");
e.printStackTrace();
return;
}

for (int i = 0; i < repeat; i++) {
System.out.println("Hello");
}

}

}
It's rare to catch a general Error or Throwable because it's actually hard to clean up after them in the general case. 


Related Discussions:- How to catch multiple exceptions explain with example

Networking, which concept is used to connect the networking in java

which concept is used to connect the networking in java

Logical operators, Logical Operators Operator ...

Logical Operators Operator Functionality E x ample/ Explanation     && Looks at 2 expressions & returns a

Idea for a project, I want to get some idea about how i can make a project ...

I want to get some idea about how i can make a project on "Free Lancer Teaching."

What does a simple spring application contain, These applications are like ...

These applications are like any Java application. They are made up of not many classes, each performing a particular purpose within the application. But these classes are configure

I need java expert with tomcat tuning experience, I need Java expert with t...

I need Java expert with tomcat tuning experience Project Description: Require a Java expert to fix memory issues with tomcat. Skills required: Java, Apache

Dialog window in Oracle by Java stored procedure, Is it possible to create ...

Is it possible to create any dialog window in Oracle in Java stored procedure?

Big m method, i''ve done an application that calculates simplex methods i n...

i''ve done an application that calculates simplex methods i need to add big m method can any one help me?

Program for extend a java based database management system, Create a compre...

Create a compressed file that contains the eclipse project directory and a short document that describes:   the methods implemented any missing or incomplete elements o

Whatsapp bulk messenger to unlimited numbers, hi,i need whatsapp panel to s...

hi,i need whatsapp panel to send unlimitd message. skype : maya123.com email :

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd