• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Compilation fails and An exception is thrown at runtime

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i do some SCJP questions
i find i didn't know when and why Compilation fails in java program
or An exception is thrown at runtime in java program
i can't clear the different Compilation fails and An exception is thrown at runtime

who can help me!?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the program doesnt conform to java , then any attempt to compile the program will cause a compilation failure. Compilation failure can be because of innumerable reason. Here is a example.
Compilation fails

javac Test.java
Undefined symbol value.
Here the variable value is not declared to be of any type[int, ....]
Hence the compilation fails here.

Runtime exception
Once the proagram is compiled succesfully java will create a class file.
Modifying above example.


Here after compiling the Test.java, Test.class is created.
Now java Test will cause a null pointer exception once the program is run at i.intValue(), because i=null. Hence its called runtime exception.
The value:10
Exception in thread "main" java.lang.NullPointerException
at Wrapper.main(Wrapper.java:13)

You really have a long way to go Best of luck.
Thanks
Deepak
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by zhang chibang:
...i can't clear the different Compilation fails and An exception is thrown at runtime...


I have to admit that was one of my bigger challenges in preparing for the SCJP exam.

When I was first learning Java (without much thought of getting certified), if I read that something would result in a compilation error or a runtime exception/error, I didn't pay much attention to the distinction. I was satisfied in simply knowing that it wouldn't work.

So when I started taking mock exams, I knew when a certain line was a problem, but I was left guessing as to whether it would fail at compile time or at runtime. My first reaction was that the mock exams were being overly critical, but...

This is an important distinction, because compilation errors must be fixed during development, while runtime problems might not appear until the code is actually in production.

In general, Sun's Java compiler is quite good at checking for potential problems. So one guideline is that if there is enough information in the source code for the compiler to check something, it will. Basically, you need to learn to "think" like the compiler, and this comes with practice. Take a lot of mock exams, and write a lot of test code. If specific cases aren't clear, post them here and we'll help you take them apart.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic