Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

why do I need exception here?

  Asked By: Ayden    Date: Jun 29    Category: Java    Views: 620

I'm coming from doing a lot of programming in python, and I'm having a
problem understanding how exceptions work in java. I looked at the
tuturial provided by Sun, and it explained that you catch exceptions in
java pretty much like you do in python:

catch NameOfException{
do something

But then why do you need to put a "throws Exception" in, as in this code
right here, which I am copying form a book?

public static void main(String[] args) throws Exception{
\\my code checks to make sure there is only on argument
int len = args.length;
if (len != 1){
System.err.println("Please provide one argument");
new Pipeline().run(args[0]);



1 Answer Found

Answer #1    Answered By: Adaulfo Fischer     Answered On: Jun 29

Java has the concept of checked exceptions  and unchecked exceptions.
Checked exceptions (those extending from Exception) require that they
are either handled at the point they occur or that the method in which
they occur is declared with the throws  clause. Unchecked exceptions
(those which extend from RuntimeException) do not need to be declared in
methods and will propagate up the call stack (this is how Python
works). Searching for "exceptions in java" will give you a lot of
reading material for learning more about java  exceptions.

Didn't find what you were looking for? Find more on why do I need exception here? Or get search suggestion and latest updates.