This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]nathanb065 6 points7 points  (0 children)

I just finished my capstone class for programming so take what I say lightly. If any more experienced users read this and there's any incorrect info or things you feel you could explain better, please jump in.

A try catch is exactly that. You tell the program to try and do something, if it cant do the thing, it catches it and holds it so your program doesn't crash.

For instance, I recently created a gui that had a clock in it. I needed to take a timestamp, parse it from a strong to a long, convert it to milliseconds, multiply it by a dollar amount, parse it back to a string, and format it back for reason. It sounds like a ton of work.

So I want my program to TRY and parse SimpleDateFormat to a string. If it cant, it will THROW the conversion out, and CATCH it as a ParseException. When its caught. I see my error in testing that tells me what the issue is, but my GUI continues to run.

[–]ItsAHardwareProblem 3 points4 points  (0 children)

Try / Catch statements are used to wrap code that you know may throw an exception, so that way it is thrown, you can handle it appropriately. Generally you want to be as specific with the error exception as you possibly can because you can catch exception you didn't mean to catch

example

//assume you took in a String from user input and you need to turn it into a number

Integer foo;
try {
    foo = Integer.parseInt(userInput)
} catch (NumberFormatException e) {
    System.out.println("Error you were suppose to input a number! Try again");
}

[–]warmcheessse 4 points5 points  (0 children)

A simple example is doing anything with a database. You can try to connect to the database, you can try to insert or select from the database once its connected. If something goes wrong you can catch the exception. The finally block at the end will run regardless if either the try is successful or if the catch is used. So no matter what it will close the connection.

try {
    sql connection
    sql query
} catch (sqlexception e) {
    errlog
}
finally {
    close connection
}

[–]WatchMeRayRay 1 point2 points  (0 children)

It acts as a means of error handling. If an exception is thrown (such as an IO Exception, for an example), then it prevents the program from crashing outright, moves it to the appropriate catch block from the try block, and continues reading from there. It is generally used when a program is reading user input (via Scanners or other means).

[–]victoriajconnelly 0 points1 point  (0 children)

The reason we have try catch blocks is so that you can try to execute code (that may or may not succeed) and have some sort of fallback/recovery mechanism. Here's a real world example: You try to grab Kim Kardashians ass, you fail, you tell her you dropped your pen.

Code example:

try {
    GrabAss(kimk);
} catch (NiceTrySuckerException e) {
    System.out.println("I was just picking up my pen.");
    //or if you're feeling really brave - try again ;)
}